char * getenv(const char *name) { int offset; return(findenv(name, &offset)); }
void compile_topology(int kflag) { extern char *compile_string(char *, int); char *so_file; int n; #if defined(APPEND_DOT_TO_LDPATH) { extern int putenv(char *); char *newenv, *oldenv; if((oldenv = findenv("LD_LIBRARY_PATH", NULL)) == (char *)NULL) newenv = strdup("LD_LIBRARY_PATH=\".\""); else { sprintf(chararray,"LD_LIBRARY_PATH=\"%s:.\"", oldenv); newenv = strdup(chararray); } putenv(newenv); } #endif for(n=0 ; n<_NNODES ; n++) { so_file = compile_string(NP[n].nattr.compile, kflag); if(nerrors) cleanup(1); dynamic_load(n, so_file); if(nerrors) cleanup(1); } }
int setenv(const char *name, const char *value, int rewrite) { static char **saveenv; /* copy of previously allocated space */ char *c, **newenv; const char *cc; size_t l_value, size; int offset; if (name == NULL || value == NULL) { errno = EINVAL; return -1; } if (*value == '=') /* no `=' in value */ ++value; l_value = strlen(value); /* find if already exists */ if ((c = findenv(name, &offset))) { if (!rewrite) return 0; if (strlen(c) >= l_value) /* old larger; copy over */ goto copy; } else { /* create new slot */ size = sizeof(char *) * (offset + 2); if (saveenv == environ) { /* just increase size */ if ((newenv = realloc(saveenv, size)) == NULL) return -1; saveenv = newenv; } else { /* get new space */ /* * We don't free here because we don't know if * the first allocation is valid on all OS's */ if ((saveenv = malloc(size)) == NULL) return -1; (void)memcpy(saveenv, environ, size - sizeof(char *)); } environ = saveenv; environ[offset + 1] = NULL; } for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */ continue; size = cc - name; /* name + `=' + value */ if ((environ[offset] = malloc(size + l_value + 2)) == NULL) return -1; c = environ[offset]; (void)memcpy(c, name, size); c += size; *c++ = '='; copy: (void)memcpy(c, value, l_value + 1); return 0; }
/*% * unsetenv(name) -- * Delete environmental variable "name". */ void unsetenv(const char *name) { char **p; int offset; while (findenv(name, &offset)) /*%< if set multiple times */ for (p = &environ[offset];; ++p) if (!(*p = *(p + 1))) break; }
char *get_env(const char *name) { pr_debug("[%s]get env name=%s\n", MODULE_NAME, name); env_init(); if (!env_valid) return NULL; return findenv(name); }
char *get_env(const char *name) { pr_notice("[%s]get env name=%s\n", MODULE_NAME, name); env_init(); if (!env_valid) { return (NULL); } return (findenv(name)); }
/** * @brief Removes an environment variable. * * @name Environment variable name. * * @todo The unsetenv() function shall fail if the name argument points to an * empty string or points to a string containing an '=' character. */ void unsetenv(char *name) { register char **P; int offset; /* If set multiple times. */ while (findenv(name, &offset)) { for (P = &environ[offset];; ++P) { if ((*P = *(P + 1)) != '\0') break; } } }
/*% * setenv -- * Set the value of the environmental variable "name" to be * "value". If rewrite is set, replace any current value. */ setenv(const char *name, const char *value, int rewrite) { extern char **environ; static int alloced; /*%< if allocated space before */ char *c; int l_value, offset; if (*value == '=') /*%< no `=' in value */ ++value; l_value = strlen(value); if ((c = findenv(name, &offset))) { /*%< find if already exists */ if (!rewrite) return (0); if (strlen(c) >= l_value) { /*%< old larger; copy over */ while (*c++ = *value++); return (0); } } else { /*%< create new slot */ int cnt; char **p; for (p = environ, cnt = 0; *p; ++p, ++cnt); if (alloced) { /*%< just increase size */ environ = (char **)realloc((char *)environ, (size_t)(sizeof(char *) * (cnt + 2))); if (!environ) return (-1); } else { /*%< get new space */ alloced = 1; /*%< copy old entries into it */ p = malloc((size_t)(sizeof(char *) * (cnt + 2))); if (!p) return (-1); memcpy(p, environ, cnt * sizeof(char *)); environ = p; } environ[cnt + 1] = NULL; offset = cnt; } for (c = (char *)name; *c && *c != '='; ++c); /*%< no `=' in name */ if (!(environ[offset] = /*%< name + `=' + value */ malloc((size_t)((int)(c - name) + l_value + 2)))) return (-1); for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); for (*c++ = '='; *c++ = *value++;); return (0); }
int unsetenv(const char *name) { char **p; int offset; if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) { errno = EINVAL; return -1; } while (findenv(name, &offset)) { /* if set multiple times */ for (p = &environ[offset];; ++p) if (!(*p = *(p + 1))) break; } return 0; }
char *find_cnetfile(char *filenm, int wantdir, int fatal) { char ch; char *env, *orig, *p, *pathname; /* FIRSTLY, TRY TO FIND THE ABSOLUTE filenm */ if(*filenm == '/') { if(access(filenm, R_OK) == 0) return( strdup(filenm) ); } else { /* ELSE, TRY TO FIND filenm USING $CNETPATH */ env = findenv("CNETPATH", CNETPATH); orig = env = strdup(env); while(*env) { while(*env == PATH_SEPARATOR) ++env; p = env; while(*p && *p != PATH_SEPARATOR) ++p; ch = *p; *p = '\0'; sprintf(chararray, "%s/%s", env, filenm); /* find this! */ if(access(chararray, R_OK) == 0) { pathname = strdup(wantdir ? env : chararray); free(orig); return(pathname); } *p = ch; env = p; } free(orig); } if(fatal) { fprintf(stderr,"%s: cannot locate \"%s\"\n", argv0,filenm); cleanup(1); /* will not return */ } return((char *)NULL); }
int unsetenv(char *name) { register int i; if (envsize < 0) { /* first time putenv called */ if (newenv() < 0) /* copy env. to heap */ return -1; } i = findenv(name); if (i < 0) return 0; /* Already here */ free(environ[i]); if (envsize > 0) envsize--; for (; environ[i]; i++) environ[i] = environ[i+1]; return 0; /* Already here */ }
int putenv(char *string) { register int i; register char *p; if (envsize < 0) { /* first time putenv called */ if (newenv() < 0) /* copy env. to heap */ return -1; } i = findenv(string); /* look for name in environment */ if (i < 0) { /* name must be added */ for (i = 0; environ[i]; i++); if (i >= (envsize - 1)) { /* need new slot */ if (moreenv() < 0) return -1; } p = malloc(strlen(string) + 1); if (p == 0) /* not enough core */ return -1; environ[i + 1] = 0; /* new end of env. */ } else { /* name already in env. */ p = realloc(environ[i], strlen(string) + 1); if (p == 0) return -1; } sprintf(p, "%s", string); /* copy into env. */ environ[i] = p; return 0; }
int __cdecl __crtsetenv ( #endif const _TSCHAR *option, const int primary ) { int ix; int remove; /* 1 if variable is to be removed */ _TSCHAR **env; _TSCHAR *name, *value; const _TSCHAR *equal; /* * check that the option string is valid, find the equal sign * and verify '=' is not the first character in string. */ if ( (option == NULL) || ((equal = _tcschr(option, _T('='))) == NULL) || option == equal) return(-1); /* if the character following '=' is null, we are removing the * the environment variable. Otherwise, we are adding or updating * an environment variable. */ remove = (*(equal + 1) == _T('\0')); /* * the first time _[w]putenv() is called, copy the environment * block that was passed to [w]main to avoid making a * dangling pointer if the block is re-alloced. */ #ifdef WPRFLAG if (_wenviron == __winitenv) _wenviron = copy_environ(_wenviron); #else if (_environ == __initenv) _environ = copy_environ(_environ); #endif /* see if requested environment array exists */ if (_tenviron == NULL) { /* * The requested type of environment does not exist. * See if other type exists, if so convert it to requested type. * The functions that convert the enviroment (__mbtow_environ and * __wtomb_environ) will call this function (__crt[w]setenv) once * for each of the pre-existing environment variables. To avoid * an infinite loop, test the primary flag. */ #ifdef WPRFLAG if (primary && _environ) { if (__mbtow_environ() != 0) return -1; } #else if (primary && _wenviron) { if (__wtomb_environ() != 0) return -1; } #endif else { /* nothing to remove, return */ if ( remove ) return 0; else { /* create ones that do not exist */ if (_environ == NULL) { if ( (_environ = _malloc_crt(sizeof(char *))) == NULL) return -1; *_environ = NULL; } if (_wenviron == NULL) { if ( (_wenviron = _malloc_crt(sizeof(wchar_t *))) == NULL) return -1; *_wenviron = NULL; } } } } /* * At this point, the two types of environments are in sync (as much * as they can be anyway). The only way they can get out of sync * (besides users directly modifiying the environment) is if there * are conversion problems: If the user sets two Unicode EVs, * "foo1" and "foo2" and converting then to multibyte yields "foo?" * and "foo?", then the environment blocks will differ. */ /* init env pointers */ env = _tenviron; /* See if the string is already in the environment */ #ifdef WPRFLAG ix = wfindenv(option, equal - option); #else ix = findenv(option, equal - option); #endif if ((ix >= 0) && (*env != NULL)) { /* String is already in the environment - overwrite/remove it */ if (remove) { /* free the string being removed */ _free_crt(env[ix]); /* removing -- move all the later strings up */ for ( ; env[ix] != NULL; ++ix) { env[ix] = env[ix+1]; } /* shrink the environment memory block (ix now has number of strings, including NULL) -- this realloc probably can't fail, since we're shrinking a mem block, but we're careful anyway. */ if (env = (_TSCHAR **) _realloc_crt(env, ix * sizeof(_TSCHAR *))) _tenviron = env; } else { /* replace the option */ env[ix] = (_TSCHAR *) option; } } else { /* * String is NOT in the environment */ if ( !remove ) { /* * Append the string to the environ table. Note that * table must be grown to do this. */ if (ix < 0) ix = -ix; /* ix = length of environ table */ if ( (env = (_TSCHAR **)_realloc_crt(env, sizeof(_TSCHAR *) * (ix + 2))) == NULL ) return -1; env[ix] = (_TSCHAR *)option; env[ix + 1] = NULL; _tenviron = env; } else /* * We are asked to remove an environment var that * isn't there...just return success */ return 0; } /* * Update the OS environment. Don't give an error if this fails * since the failure will not affect the user unless he/she is making * direct API calls. Only need to do this for one type, OS converts * to other type automatically. */ if ( primary && (name = (_TSCHAR *)_malloc_crt((_tcslen(option) + 2) * sizeof(_TSCHAR))) != NULL ) { _tcscpy(name, option); value = name + (equal - option); *value++ = _T('\0'); SetEnvironmentVariable(name, remove ? NULL : value); _free_crt(name); } return 0; }
int _RTL_FUNC _spawnlpe(int mode,const char *path, const char *argv0,...) { return spawnbase(path,&argv0,findenv(&argv0),mode,1); }
int _RTL_FUNC _execlpe(const char *path, const char *argv0,...) { return spawnbase(path,&argv0,findenv(&argv0),P_OVERLAY,1); }
static int make_ofile(int kflag, char **Cflags, char *o_file, char *c_file) { #if !defined(USE_WIN32) int pid; #endif struct stat stat_c, stat_o; static char *cnp = (char *)NULL; char *av[64]; /* hope that this is enough! */ int status; int ac, i; /* FIRSTLY, ENSURE THAT THE SOURCEFILE EXISTS AND NEEDS RECOMPILING */ if(stat(c_file, &stat_c) == -1) { fprintf(stderr,"%s: cannot find sourcefile %s\n",argv0,c_file); ++nerrors; return(-1); } if(stat(o_file, &stat_o) == 0 && stat_c.st_mtime <= stat_o.st_mtime) return(0); #if CHECK_RECEIVE_SPELLING if(spell_check(c_file) == FALSE) return(-1); #endif if(cnp == (char *)NULL) { cnp = find_cnetfile("cnet.h", TRUE, TRUE); if(vflag) fprintf(stderr,"using include directory \"%s\"\n", cnp); } #if defined(USE_WIN32) ac = 0; av[ac++] = "CL"; av[ac++] = OS_DEFINE; av[ac++] = "/DHAVE_LONG_LONG=1"; sprintf(chararray, "/DSIZEOF_INT=%d", sizeof(int)); av[ac++] = strdup(chararray); sprintf(chararray, "/DSIZEOF_LONG=%d", sizeof(long)); av[ac++] = strdup(chararray); sprintf(chararray, "/I%s", cnp); av[ac++] = strdup(chararray); while(*Cflags) /* add C compiler switches */ av[ac++] = *Cflags++; av[ac++] = "/c"; sprintf(chararray, "/Fo%s", o_file); av[ac++] = strdup(chararray); if(!vflag) av[ac++] = "/NOLOGO"; av[ac++] = c_file; av[ac ] = NULL; if(dflag) { fputs(av[0], stderr); for(i=1 ; i<ac ; i++) fprintf(stderr," %s",av[i]); fputc('\n',stderr); } else fprintf(stderr,"compiling %s\n", c_file); status = _spawnvp(_P_WAIT, av[0], &av[1]); if(status != 0) { if(status == -1) fprintf(stderr,"%s: spawn of %s unsuccessful: %s\n", argv0,av[0],_sys_errlist[(int)errno]); exit(1); } #else switch (pid = fork()) { case -1 : fprintf(stderr,"%s: cannot fork\n",argv0); exit(1); break; case 0 : ac = 0; #if USE_GCC_COMPILER av[ac++] = findenv("CNETGCC", CNETGCC); av[ac++] = "gcc"; if(!kflag) /* not using "old" K&R C */ av[ac++] = "-ansi"; #if GCC_WERROR_WANTED av[ac++] = "-Werror"; #endif #if GCC_WALL_WANTED av[ac++] = "-Wall"; #endif #else av[ac++] = findenv("CNETCC", CNETCC); av[ac++] = "cc"; #endif ac = add_compile_args(ac, av, kflag); av[ac++] = OS_DEFINE; #if HAVE_LONG_LONG av[ac++] = "-DHAVE_LONG_LONG=1"; #endif sprintf(chararray, "-DSIZEOF_INT=%d", sizeof(int)); av[ac++] = strdup(chararray); sprintf(chararray, "-DSIZEOF_LONG=%d", sizeof(long)); av[ac++] = strdup(chararray); while(*Cflags) /* add C compiler switches */ av[ac++] = *Cflags++; sprintf(chararray, "-I%s", cnp); av[ac++] = strdup(chararray); av[ac++] = "-c"; av[ac++] = "-o"; av[ac++] = o_file; av[ac++] = c_file; av[ac ] = NULL; if(dflag) { fputs(av[0], stderr); for(i=2 ; i<ac ; i++) fprintf(stderr," %s",av[i]); fputc('\n',stderr); } else fprintf(stderr,"compiling %s\n", c_file); execvp(av[0], &av[1]); fprintf(stderr,"%s: cannot exec %s\n",argv0,av[0]); exit(1); break; default : while(wait(&status) != pid) ; if(status != 0) exit(1); break; } #endif return(0); }