3 % (c) The GRASP/AQUA Project, Glasgow University, 1995-1996
5 \subsection[env.lc]{Environment Handling for LibPosix}
7 Many useful environment functions are not necessarily provided by libc.
8 To get around this problem, we introduce our own. The first time that
9 you modify your environment, we copy the environment wholesale into
10 malloc'ed locations, so that subsequent modifications can do proper
11 memory management. The $environ$ variable is updated with a pointer
12 to the current environment so that the normal $getenv$ and $exec*$ functions
13 should continue to work properly.
21 /* Switch this on once we've moved the environment to the malloc arena */
25 * For some reason, OSF turns off the prototype for this if we're
26 * _POSIX_SOURCE. Seems to me that this ought to be an ANSI-ism
27 * rather than a POSIX-ism, but no matter. (JSM(?))
31 strDup(const char *src)
33 int len = strlen(src) + 1;
36 if ((dst = malloc(len)) != NULL)
37 memcpy(dst, src, len);
41 /* Replace the entire environment */
43 setenviron(char **envp)
46 int dirtyOld = dirtyEnv;
49 /* A quick hack to move the strings out of the heap */
55 /* Release the old space if we allocated it ourselves earlier */
57 for (i = 0; old[i] != NULL; i++)
64 /* Copy initial environment into malloc arena */
71 for (i = 0; environ[i] != NULL; i++)
74 if ((new = (char **) malloc((i + 1) * sizeof(char *))) == NULL)
80 if ((new[i] = strDup(environ[i])) == NULL) {
81 while (new[++i] != NULL)
92 /* Set or replace an environment variable
93 * simonm 14/2/96 - this is different to the standard C library
94 * implementation and the prototypes clash, so I'm calling it _setenv.
97 _setenv(char *mapping)
103 /* We must have a non-empty key and an '=' */
104 if (mapping[0] == '=' || (p = strchr(mapping, '=')) == NULL) {
108 /* Include through the '=' for matching */
109 keylen = p - mapping + 1;
111 if (!dirtyEnv && copyenv() != 0)
114 if ((p = strDup(mapping)) == NULL)
117 /* Look for an existing key that matches */
118 for (i = 0; environ[i] != NULL && strncmp(environ[i], p, keylen) != 0; i++);
120 if (environ[i] != NULL) {
124 /* We want to grow the table by *two*, one for the new entry, one for the terminator */
125 if ((new = (char **) realloc((void*)environ, (i + 2) * sizeof(char *))) == NULL) {
136 /* Delete a variable from the environment */
142 if (strchr(name, '=') != NULL) {
146 keylen = strlen(name);
148 if (!dirtyEnv && copyenv() != 0)
151 /* Look for a matching key */
152 for (i = 0; environ[i] != NULL &&
153 (strncmp(environ[i], name, keylen) != 0 || environ[i][keylen] != '='); i++);
155 /* Don't complain if it wasn't there to begin with */
156 if (environ[i] == NULL) {
162 environ[i] = environ[i + 1];
164 } while (environ[i] != NULL);