/* * setpath - Set the PATH environment variable for the GEM desktop. * by Dale Schumacher */ #include #include extern char *_envp; #define TOS_STYLE 1 /* add TOS-style variables */ static char *findenv(var) register char *var; /* * INTERNAL FUNCTION. This functions attempts to locate in * the environment. If is not found, NULL is returned. If * the environment is NULL, NULL is returned. If is found, * a pointer to the beginning of in the environment is returned. * BOTH MS-DOS AND TOS ENVIRONMENT FORMATS ARE ACCOMODATED. */ { register char *p; register int len; if((p = _envp) == NULL) return(NULL); len = strlen(var); while(*p) { if((p[len] == '=') && !strncmp(p, var, len)) return(p); while(*p++) /* move to next arg */ ; } return(NULL); } char *getenv(var) register char *var; /* * Search for in the environment. If is found, a pointer * to it's value is returned. NULL is returned if is not found. * WARNING: The returned pointer points into the environment and * must not be modified! */ { register char *p, *q; register int len; len = strlen(var); if(p = findenv(var)) { p += (len + 1); if(*p == '\0') /* TOS env format or empty value */ { q = p+1; if(*q == '\0') /* empty value + end of env */ return(p); while(*q && (*q != '=')) ++q; if(*q) /* empty value */ return(p); else /* TOS env format */ return(p+1); } } return(p); } int putenv(entry) char *entry; /* * Add to the environment. can be any of the following * forms: * * = * = * The first form removes from the environment. getenv() * will return NULL if looking for this variable. The second form adds * to the environment, with a null value. getenv() will * return a pointer to a '\0' character if looking for this variable. * Many environment handlers don't support such "tag variables", so * their use is not recommended. The final form is the most common, * and safest to use. is installed (or replaced) with the * value . It should be noted that this function itself is not * supported in many systems and should be used will care to prevent * overflowing the space allocated for the environment. */ { register char *p, *q, *t, c; register int len; for(t=entry; (c = *t) && (c != '='); ++t) ; *t = '\0'; if(p = getenv(entry)) /* remove old var */ { q = p; while(*q++) /* find end of old val */ ; p -= (len = strlen(entry)); while(strncmp(--p, entry, len)) /* find start of old var */ ; while(*q) /* copy environment tail */ while(*p++ = *q++) ; *p++ = '\0'; /* tie off environment */ *p = 0xFF; } if(c == '=') /* install new var */ { p = _envp; while(*p) /* find end of env */ while(*p++) ; #if MWC_STYLE *t = c; q = entry; while(*p++ = *q++) /* copy new entry */ ; #endif #if TOS_STYLE q = entry; while(*p++ = *q++) /* copy new var */ ; *--p = '='; *++p = '\0'; while(*p++ = *q++) /* copy new value */ ; *t = c; #endif *p++ = '\0'; /* tie off environment */ *p = 0xFF; } return(TRUE); } main(argc, argv) int argc; char *argv[]; /* * Assume that this program is being called from the desktop. * To change the PATH for the desktop, you modify the environment * of the parent process (the desktop). */ { char path[256]; register BASEPAGE *bp; if(argc != 2) { fprintf(stderr, "usage: SETPATH \n"); getch(); exit(EXIT_FAILURE); } sprintf(path, "PATH=%s", argv[1]); bp = _base->p_parent; /* locate parent's basepage */ _envp = bp->p_env; /* get environment pointer */ putenv(path); }