1 --- setenv.c.orig 2006-12-12 18:14:46.000000000 -0800
2 +++ setenv.c 2006-12-12 18:22:12.000000000 -0800
7 +#include <crt_externs.h>
9 +#include <sys/types.h>
11 +#include <malloc/malloc.h>
13 -char *__findenv(const char *, int *);
14 +#define ZONE_OWNS_PTR(zone, ptr) (malloc_zone_from_ptr((ptr)) == zone)
16 +extern malloc_zone_t *__zone0;
17 +extern void __malloc_check_env_name(const char *);
19 +__private_extern__ char *__findenv(const char *, int *, char **);
20 +__private_extern__ int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t *);
21 +__private_extern__ void __unsetenv(const char *, char **, malloc_zone_t *);
23 +#ifndef BUILDING_VARIANT
26 - * Set the value of the environmental variable "name" to be
27 - * "value". If rewrite is set, replace any current value.
28 + * The copy flag may have 3 values:
29 + * 1 - make a copy of the name/value pair
30 + * 0 - take the name as a user-supplied name=value string
31 + * -1 - like 0, except we copy of the name=value string in name
34 -setenv(name, value, rewrite)
35 +__private_extern__ int
36 +__setenv(name, value, rewrite, copy, environp, envz)
42 + malloc_zone_t *envz;
44 - extern char **environ;
45 - static char **alloced; /* if allocated space before */
47 - int l_value, offset;
50 - if (*value == '=') /* no `=' in value */
52 - l_value = strlen(value);
53 - if ((c = __findenv(name, &offset))) { /* find if already exists */
54 + if ((c = __findenv(name, &offset, *environp))) { /* find if already exists */
58 - if (strlen(c) >= l_value) { /* old larger; copy over */
60 + * In UNIX03, we can overwrite only if we allocated the
61 + * string. Then we can realloc it if it is too small.
63 + e = (*environp)[offset];
64 + if (copy > 0 && ZONE_OWNS_PTR(envz, e)) {
65 + size_t l_value = strlen(value);
66 + if (strlen(c) < l_value) { /* old smaller; resize*/
69 + if ((r = realloc(e, l_value + len + 1)) == NULL)
72 + (*environp)[offset] = r;
76 while ( (*c++ = *value++) );
83 - for (p = environ, cnt = 0; *p; ++p, ++cnt);
84 - if (alloced == environ) { /* just increase size */
85 - p = (char **)realloc((char *)environ,
86 + for (p = *environp, cnt = 0; *p; ++p, ++cnt);
87 + if (ZONE_OWNS_PTR(envz, *environp)) { /* just increase size */
88 + p = (char **)realloc((char *)*environp,
89 (size_t)(sizeof(char *) * (cnt + 2)));
92 - alloced = environ = p;
95 else { /* get new space */
96 /* copy old entries into it */
97 - p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
98 + p = malloc_zone_malloc(envz, (size_t)(sizeof(char *) * (cnt + 2)));
101 - bcopy(environ, p, cnt * sizeof(char *));
102 - alloced = environ = p;
103 + bcopy(*environp, p, cnt * sizeof(char *));
106 - environ[cnt + 1] = NULL;
107 + (*environp)[cnt + 1] = NULL;
110 - for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
111 - if (!(environ[offset] = /* name + `=' + value */
112 - malloc((size_t)((int)(c - name) + l_value + 2))))
114 - for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
115 - for (*c++ = '='; (*c++ = *value++); );
116 + /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
117 + * strings. For Unix03 putenv(), we put the string directly in
118 + * the environment. */
120 + for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
121 + if (!((*environp)[offset] = /* name + `=' + value */
122 + malloc_zone_malloc(envz, (size_t)((int)(c - name) + strlen(value) + 2))))
124 + for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
125 + for (*c++ = '='; (*c++ = *value++); );
127 + /* the legacy behavior copies the string */
129 + size_t len = strlen(name);
130 + if((c = malloc_zone_malloc(envz, len + 1)) == NULL)
132 + memcpy(c, name, len + 1);
135 + /* if we malloc-ed the previous value, free it first */
136 + if ((*environp)[offset] != NULL && ZONE_OWNS_PTR(envz, (*environp)[offset]))
137 + free((*environp)[offset]);
138 + (*environp)[offset] = (char *)name;
143 +__private_extern__ void
144 +__unsetenv(const char *name, char **environ, malloc_zone_t *envz)
149 + while (__findenv(name, &offset, environ)) { /* if set multiple times */
150 + /* if we malloc-ed it, free it first */
151 + if (ZONE_OWNS_PTR(envz, environ[offset]))
152 + free(environ[offset]);
153 + for (p = &environ[offset];; ++p)
154 + if (!(*p = *(p + 1)))
159 +/****************************************************************************/
161 + * _allocenvstate -- SPI that creates a new state (opaque)
164 +_allocenvstate(void)
166 + return (void *)malloc_create_zone(1000 /* unused */, 0 /* unused */);
170 + * _copyenv -- SPI that copies a NULL-tereminated char * array in a newly
171 + * allocated buffer, compatible with the other SPI env routines. If env
172 + * is NULL, a char * array composed of a single NULL is returned. NULL
173 + * is returned on error. (This isn't needed anymore, as __setenv will
174 + * automatically make a copy in the zone.)
177 +_copyenv(char **env)
183 + for (p = env; *p; ++p, ++cnt);
184 + p = (char **)malloc((size_t)(sizeof(char *) * cnt));
188 + bcopy(env, p, cnt * sizeof(char *));
195 + * _deallocenvstate -- SPI that frees all the memory associated with the state
196 + * and all allocated strings, including the environment array itself if it
200 +_deallocenvstate(void *state)
202 + malloc_zone_t *envz;
204 + if (!(envz = (malloc_zone_t *)state) || envz == __zone0) {
208 + malloc_destroy_zone(envz);
213 + * setenvp -- SPI using an arbitrary pointer to string array and an env state,
214 + * created by _allocenvstate(). Initial checking is not done.
216 + * Set the value of the environmental variable "name" to be
217 + * "value". If rewrite is set, replace any current value.
220 +_setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state)
222 + /* insure __zone0 is set up */
224 + __zone0 = malloc_create_zone(0, 0);
230 + return (__setenv(name, value, rewrite, 1, envp, (state ? (malloc_zone_t *)state : __zone0)));
234 + * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
235 + * state, created by _allocenvstate(). Initial checking is not done.
237 + * Delete environmental variable "name".
240 +_unsetenvp(const char *name, char ***envp, void *state)
242 + /* insure __zone0 is set up */
244 + __zone0 = malloc_create_zone(0, 0);
250 + __unsetenv(name, *envp, (state ? (malloc_zone_t *)state : __zone0));
254 +#endif /* !BUILD_VARIANT */
258 + * Set the value of the environmental variable "name" to be
259 + * "value". If rewrite is set, replace any current value.
262 +setenv(name, value, rewrite)
267 + /* no null ptr or empty str */
268 + if(name == NULL || *name == 0) {
274 + /* no '=' in name */
275 + if (strchr(name, '=')) {
279 +#endif /* __DARWIN_UNIX03 */
281 + if (*value == '=') /* no `=' in value */
283 + /* insure __zone0 is set up before calling __malloc_check_env_name */
285 + __zone0 = malloc_create_zone(0, 0);
291 + __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
292 + return (__setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0));
297 * Delete environmental variable "name".
301 +#else /* !__DARWIN_UNIX03 */
303 +#endif /* __DARWIN_UNIX03 */
307 - extern char **environ;
311 + /* no null ptr or empty str */
312 + if(name == NULL || *name == 0) {
317 - while (__findenv(name, &offset)) /* if set multiple times */
318 - for (p = &environ[offset];; ++p)
319 - if (!(*p = *(p + 1)))
321 + /* no '=' in name */
322 + if (strchr(name, '=')) {
326 + /* insure __zone0 is set up before calling __malloc_check_env_name */
328 + __zone0 = malloc_create_zone(0, 0);
334 +#else /* !__DARWIN_UNIX03 */
335 + /* no null ptr or empty str */
336 + if(name == NULL || *name == 0)
338 + /* insure __zone0 is set up before calling __malloc_check_env_name */
340 + __zone0 = malloc_create_zone(0, 0);
344 +#endif /* __DARWIN_UNIX03 */
345 + __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
346 + __unsetenv(name, *_NSGetEnviron(), __zone0);
349 +#endif /* __DARWIN_UNIX03 */