replacement for putenv()
Leo de Wit
leo at philmds.UUCP
Sun Feb 12 05:12:21 AEST 1989
In article <549 at marob.MASA.COM> rogol at marob.masa.com (Fred Buck) writes:
[]
| /* see if this variable is already in environment */
| i = (-1);
| while (environ[++i]) {
| if (strncmp(string,environ[i],namelen)==0) { /* It's there */
| /* if we can just patch it, do so and return */
| if (strlen(string)<=strlen(environ[i])) {
| strcpy(environ[i],string); return(0);
| }
| else break;
| }
| }
| /* OK, allocate a spot for 'string' and copy it there */
| cptr = malloc(strlen(string)+1);
| if (cptr==0) return(-1); /* can't malloc */
| strcpy(cptr,string);
| /* no env at all; or else var exists but's too small to hold 'string' */
| if (i==0 || environ[i]) {
| environ[i] = cptr; return(0); /* ok, done */
| }
If i == 0 (no env at all), you end up overwriting the terminating null
ptr (environ[0]). So the last three lines should read:
if (environ[i] != (char *)0) { /* or simply: if (environ[i]) */
environ[i] = cptr; return(0); /* ok, done */
}
Now for i == 0 a array for two char ptrs is allocated in the code that follows.
Leo.
More information about the Comp.unix.wizards
mailing list