Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!topaz.rutgers.edu!ron From: ron@topaz.rutgers.edu (Ron Natalie) Newsgroups: comp.unix.wizards Subject: Re: Adding to Environment Pointers Message-ID: <12864@topaz.rutgers.edu> Date: Tue, 23-Jun-87 11:10:42 EDT Article-I.D.: topaz.12864 Posted: Tue Jun 23 11:10:42 1987 Date-Received: Thu, 25-Jun-87 01:16:02 EDT References: <7950@brl-adm.ARPA> Organization: Rutgers Univ., New Brunswick, N.J. Lines: 86 This one is simple so I'll just include the code: /* * Environment diddlers * * Ron Natalie. */ extern char **environ; /* Pointer to environment, set by crt0 * used by execv, execl */ static int _set_env = 0; /* When zero, indicates that the environ * points to the original stack resident * environment. We set it to one so that * we can free memory we malloc'd on * subsequent calls to setenv. */ /* * setenv - add an element to the environment * * var is added to the environment. It should be a string of * the form "NAME=value". Note, that the actual string is not * copied, but a pointer is used, so be careful not to overwrite * it before the exec. Also, this does not check to make sure * one doesn't already exist, so delenv first if you're not sure. */ setenv(var) char *var; { char **e, **n; int count; char **new; /* * Count the number of items in the environment and malloc * room for all those plus the one we are adding. */ for(e = environ, count = 0; *e; e++) count++; new = (char **)malloc( (count+2)*sizeof (char *)); /* * Copy over the points from the old to the new, and add the * new one. */ for(e = environ, n = new; count; count--) *n++ = *e++; *n++ = var; *n = 0; /* * If we had allocated this environ from a previous call, * free it now. */ if(_set_env) free(environ); else _set_env = 1; environ = new; } /* * delenv - delete an environment variable. * * Var should be the name of an environment variable such as "PATH" * Note, that the space is not reclaimed, but this doesn't happen all * that much. */ delenv(var) char *var; { char **e; int len = strlen(var); /* * Look for the entry in the environment. */ e = environ; while(*e) { if(strncmp(var, *e, len) == 0) break; e++; } /* * Move everything after it, including the null terminator * down one, obliterating the deleted pointer. while(*e) { *e = e[1]; e++; } }