Path: utzoo!attcan!uunet!lll-winken!xanth!nic.MR.NET!shamash!com50!pwcs!stag!daemon From: to_stdnet@stag.UUCP Newsgroups: comp.sys.atari.st Subject: setenv on boot-up (how?) Message-ID: <747@stag.UUCP> Date: 19 Mar 89 08:14:03 GMT Sender: news@stag.UUCP Lines: 206 From: dal@syntel.UUCP (Dale Schumacher) [csrobe@ICASE.EDU (Charles S. [Chip] Roberson) writes...] > I'm almost sure this problem has been discussed, answered, and a program > supplied, but I can't remember the answer and can't find the program. > I would like to set some environment variables upon boot-up to last > for as long as the ST is on. How do I do this? The problem here is "at boot-up". I assume you want a program to put in the /AUTO/ folder that will set the desired environment variables. The problem is, when the /AUTO/ folder programs are being run, the desktop has not yet been started. This fact is how the following function determines if a program is running from the /AUTO/ folder, the desktop or some child of the desktop (a shell). camefrom() { register BASEPAGE *p = _base; register int n = 0; p = p->p_parent; if((((long) (p->p_tbase)) & 0x800000L) == 0L) return(FROM_SHELL); while(p = p->p_parent) ++n; return((n == 1) ? FROM_AUTO : ((n == 2) ? FROM_DESKTOP : FROM_SHELL)); } To do what you want requires you to set the desktop's environment, which is obviously not possible until the desktop is running. However, once the desktop IS running, the following program will set the PATH variable as an example of how the desktop's environment can be changed. Note that this program MUST BE RUN FROM THE DESKTOP to work properly. It should probably contain a call to camefrom() to verify this. /* * setpath - Set the PATH environment variable for the GEM desktop. * by Dale Schumacher */ #include #include extern char *_envp; /* pick ONLY ONE of the following two choices */ #define TOS_STYLE 1 /* add TOS-style variables */ #define MWC_STYLE 0 /* add MWC-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); } -- Dale Schumacher 399 Beacon Ave. (alias: Dalnefre') St. Paul, MN 55104 ...bungia!cctb!syntel!dal United States of America "I may be competitive, but I'm never ruthless"