1 --- setenv.c.orig 2011-04-13 01:21:14.000000000 -0700
2 +++ setenv.c 2011-04-13 14:44:04.000000000 -0700
3 @@ -36,32 +36,115 @@ __FBSDID("$FreeBSD: src/lib/libc/stdlib/
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 *);
22 +__private_extern__ int init__zone0(int);
26 - * Set the value of the environmental variable "name" to be
27 - * "value". If rewrite is set, replace any current value.
28 + * _cthread_init_routine used to be called from crt1.o to initialize threads.
29 + * This is no longer needed, as initialization happens in dylib initializers,
30 + * but is provided to maintain backwards compatibility. Normally, for 10.5
31 + * or greater, _cthread_init_routine does nothing.
33 + * Before 10.5, the _start routine in crt1.o clobbers environ with the original
34 + * stack value, which effectively undoes any environment changes made in
35 + * initializers. When LEGACY_CRT1_ENVIRON is defined, we replace the
36 + * do-nothing routine with one that attempts to restore the environ value.
37 + * But this only works if the setenv (and family) routines were used
38 + * exclusively, (no direct manipulation of environ). Note that according to
39 + * SUSv3, direct manipulation of environ may result in undefined behavior in
40 + * setenv and family, so we don't support that (on less than 10.5).
43 -setenv(name, value, rewrite)
44 +#ifdef BUILDING_VARIANT
45 +# ifdef LEGACY_CRT1_ENVIRON
46 +extern char **_saved_environ;
47 +# endif /* LEGACY_CRT1_ENVIRON */
48 +#else /* !BUILDING_VARIANT */
49 +# ifdef LEGACY_CRT1_ENVIRON
50 +__private_extern__ char **_saved_environ = NULL;
53 +_legacy_crt1_environ(void)
55 + if (_saved_environ) *_NSGetEnviron() = _saved_environ;
58 +int (*_cthread_init_routine)(void) = _legacy_crt1_environ;
60 +# else /* !LEGACY_CRT1_ENVIRON */
61 +static int _do_nothing(void) { return 0; }
62 +int (*_cthread_init_routine)(void) = _do_nothing;
63 +# endif /* !LEGACY_CRT1_ENVIRON */
66 + * Create the environment malloc zone and give it a recognizable name.
68 +__private_extern__ int
69 +init__zone0(int should_set_errno)
71 + if (__zone0) return (0);
73 + __zone0 = malloc_create_zone(0, 0);
75 + if (should_set_errno) {
80 + malloc_set_zone_name(__zone0, "environ");
85 + * The copy flag may have 3 values:
86 + * 1 - make a copy of the name/value pair
87 + * 0 - take the name as a user-supplied name=value string
88 + * -1 - like 0, except we copy of the name=value string in name
90 +__private_extern__ int
91 +__setenv(name, value, rewrite, copy, environp, envz)
97 + malloc_zone_t *envz;
99 - extern char **environ;
100 - static char **alloced; /* if allocated space before */
102 - int l_value, offset;
105 - if (*value == '=') /* no `=' in value */
107 - l_value = strlen(value);
108 - if ((c = __findenv(name, &offset))) { /* find if already exists */
109 + if ((c = __findenv(name, &offset, *environp))) { /* find if already exists */
113 - if (strlen(c) >= l_value) { /* old larger; copy over */
115 + * In UNIX03, we can overwrite only if we allocated the
116 + * string. Then we can realloc it if it is too small.
118 + e = (*environp)[offset];
119 + if (copy > 0 && ZONE_OWNS_PTR(envz, e)) {
120 + size_t l_value = strlen(value);
121 + if (strlen(c) < l_value) { /* old smaller; resize*/
123 + size_t len = c - e;
124 + if ((r = realloc(e, l_value + len + 1)) == NULL)
127 + (*environp)[offset] = r;
131 while ( (*c++ = *value++) );
134 @@ -69,48 +152,235 @@ setenv(name, value, rewrite)
138 - for (p = environ, cnt = 0; *p; ++p, ++cnt);
139 - if (alloced == environ) { /* just increase size */
140 - p = (char **)realloc((char *)environ,
141 + for (p = *environp, cnt = 0; *p; ++p, ++cnt);
142 + if (ZONE_OWNS_PTR(envz, *environp)) { /* just increase size */
143 + p = (char **)realloc((char *)*environp,
144 (size_t)(sizeof(char *) * (cnt + 2)));
147 - alloced = environ = p;
150 else { /* get new space */
151 /* copy old entries into it */
152 - p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
153 + p = malloc_zone_malloc(envz, (size_t)(sizeof(char *) * (cnt + 2)));
156 - bcopy(environ, p, cnt * sizeof(char *));
157 - alloced = environ = p;
158 + bcopy(*environp, p, cnt * sizeof(char *));
161 - environ[cnt + 1] = NULL;
162 + (*environp)[cnt + 1] = NULL;
165 - for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
166 - if (!(environ[offset] = /* name + `=' + value */
167 - malloc((size_t)((int)(c - name) + l_value + 2))))
169 - for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
170 - for (*c++ = '='; (*c++ = *value++); );
171 + /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
172 + * strings. For Unix03 putenv(), we put the string directly in
173 + * the environment. */
175 + for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
176 + if (!((*environp)[offset] = /* name + `=' + value */
177 + malloc_zone_malloc(envz, (size_t)((int)(c - name) + strlen(value) + 2))))
179 + for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
180 + for (*c++ = '='; (*c++ = *value++); );
182 + /* the legacy behavior copies the string */
184 + size_t len = strlen(name);
185 + if((c = malloc_zone_malloc(envz, len + 1)) == NULL)
187 + memcpy(c, name, len + 1);
190 + /* if we malloc-ed the previous value, free it first */
191 + if ((*environp)[offset] != NULL && ZONE_OWNS_PTR(envz, (*environp)[offset]))
192 + free((*environp)[offset]);
193 + (*environp)[offset] = (char *)name;
198 +__private_extern__ void
199 +__unsetenv(const char *name, char **environ, malloc_zone_t *envz)
204 + while (__findenv(name, &offset, environ)) { /* if set multiple times */
205 + /* if we malloc-ed it, free it first */
206 + if (ZONE_OWNS_PTR(envz, environ[offset]))
207 + free(environ[offset]);
208 + for (p = &environ[offset];; ++p)
209 + if (!(*p = *(p + 1)))
214 +/****************************************************************************/
216 + * _allocenvstate -- SPI that creates a new state (opaque)
219 +_allocenvstate(void)
221 + malloc_zone_t *zone;
222 + zone = malloc_create_zone(1000 /* unused */, 0 /* unused */);
224 + malloc_set_zone_name(zone, "environ");
226 + return (void *)zone;
230 + * _copyenv -- SPI that copies a NULL-tereminated char * array in a newly
231 + * allocated buffer, compatible with the other SPI env routines. If env
232 + * is NULL, a char * array composed of a single NULL is returned. NULL
233 + * is returned on error. (This isn't needed anymore, as __setenv will
234 + * automatically make a copy in the zone.)
237 +_copyenv(char **env)
243 + for (p = env; *p; ++p, ++cnt);
244 + p = (char **)malloc((size_t)(sizeof(char *) * cnt));
248 + bcopy(env, p, cnt * sizeof(char *));
255 + * _deallocenvstate -- SPI that frees all the memory associated with the state
256 + * and all allocated strings, including the environment array itself if it
260 +_deallocenvstate(void *state)
262 + malloc_zone_t *envz;
264 + if (!(envz = (malloc_zone_t *)state) || envz == __zone0) {
268 + malloc_destroy_zone(envz);
273 + * setenvp -- SPI using an arbitrary pointer to string array and an env state,
274 + * created by _allocenvstate(). Initial checking is not done.
276 + * Set the value of the environmental variable "name" to be
277 + * "value". If rewrite is set, replace any current value.
280 +_setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state)
282 + if (init__zone0(1)) return (-1);
283 + return (__setenv(name, value, rewrite, 1, envp, (state ? (malloc_zone_t *)state : __zone0)));
287 + * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
288 + * state, created by _allocenvstate(). Initial checking is not done.
290 + * Delete environmental variable "name".
293 +_unsetenvp(const char *name, char ***envp, void *state)
295 + if (init__zone0(1)) return (-1);
296 + __unsetenv(name, *envp, (state ? (malloc_zone_t *)state : __zone0));
300 +#endif /* !BUILD_VARIANT */
304 + * Set the value of the environmental variable "name" to be
305 + * "value". If rewrite is set, replace any current value.
308 +setenv(name, value, rewrite)
313 +#ifdef LEGACY_CRT1_ENVIRON
315 +#endif /* LEGACY_CRT1_ENVIRON */
317 + /* no null ptr or empty str */
318 + if(name == NULL || *name == 0) {
324 + /* no '=' in name */
325 + if (strchr(name, '=')) {
329 +#endif /* __DARWIN_UNIX03 */
331 + if (*value == '=') /* no `=' in value */
333 + /* insure __zone0 is set up before calling __malloc_check_env_name */
334 + if (init__zone0(1)) return (-1);
335 + __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
336 +#ifdef LEGACY_CRT1_ENVIRON
337 + ret = __setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0);
338 + _saved_environ = *_NSGetEnviron();
340 +#else /* !LEGACY_CRT1_ENVIRON */
341 + return (__setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0));
342 +#endif /* !LEGACY_CRT1_ENVIRON */
347 * Delete environmental variable "name".
351 +#else /* !__DARWIN_UNIX03 */
353 +#endif /* __DARWIN_UNIX03 */
357 - extern char **environ;
361 + /* no null ptr or empty str */
362 + if(name == NULL || *name == 0) {
367 - while (__findenv(name, &offset)) /* if set multiple times */
368 - for (p = &environ[offset];; ++p)
369 - if (!(*p = *(p + 1)))
371 + /* no '=' in name */
372 + if (strchr(name, '=')) {
376 + /* insure __zone0 is set up before calling __malloc_check_env_name */
377 + if (init__zone0(1)) return (-1);
378 +#else /* !__DARWIN_UNIX03 */
379 + /* no null ptr or empty str */
380 + if(name == NULL || *name == 0)
382 + /* insure __zone0 is set up before calling __malloc_check_env_name */
383 + if (init__zone0(0)) return;
384 +#endif /* __DARWIN_UNIX03 */
385 + __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
386 + __unsetenv(name, *_NSGetEnviron(), __zone0);
389 +#endif /* __DARWIN_UNIX03 */