]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/setenv.c.patch
Libc-498.tar.gz
[apple/libc.git] / stdlib / FreeBSD / setenv.c.patch
1 --- setenv.c.orig 2006-12-12 18:14:46.000000000 -0800
2 +++ setenv.c 2006-12-12 18:22:12.000000000 -0800
3 @@ -40,32 +40,60 @@
4 #include <stddef.h>
5 #include <stdlib.h>
6 #include <string.h>
7 +#include <crt_externs.h>
8 +#include <errno.h>
9 +#include <sys/types.h>
10 +#include <fcntl.h>
11 +#include <malloc/malloc.h>
12
13 -char *__findenv(const char *, int *);
14 +#define ZONE_OWNS_PTR(zone, ptr) (malloc_zone_from_ptr((ptr)) == zone)
15
16 +extern malloc_zone_t *__zone0;
17 +extern void __malloc_check_env_name(const char *);
18 +
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 +
23 +#ifndef BUILDING_VARIANT
24 /*
25 - * setenv --
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
32 */
33 -int
34 -setenv(name, value, rewrite)
35 +__private_extern__ int
36 +__setenv(name, value, rewrite, copy, environp, envz)
37 const char *name;
38 const char *value;
39 - int rewrite;
40 + int rewrite, copy;
41 + char ***environp;
42 + malloc_zone_t *envz;
43 {
44 - extern char **environ;
45 - static char **alloced; /* if allocated space before */
46 char *c;
47 - int l_value, offset;
48 + int offset;
49
50 - if (*value == '=') /* no `=' in value */
51 - ++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 */
55 + char *e;
56 if (!rewrite)
57 return (0);
58 - if (strlen(c) >= l_value) { /* old larger; copy over */
59 + /*
60 + * In UNIX03, we can overwrite only if we allocated the
61 + * string. Then we can realloc it if it is too small.
62 + */
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*/
67 + char *r;
68 + size_t len = c - e;
69 + if ((r = realloc(e, l_value + len + 1)) == NULL)
70 + return (-1);
71 + if (r != e) {
72 + (*environp)[offset] = r;
73 + c = r + len;
74 + }
75 + }
76 while ( (*c++ = *value++) );
77 return (0);
78 }
79 @@ -73,48 +101,250 @@
80 int cnt;
81 char **p;
82
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)));
90 if (!p)
91 return (-1);
92 - alloced = environ = p;
93 + *environp = p;
94 }
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)));
99 if (!p)
100 return (-1);
101 - bcopy(environ, p, cnt * sizeof(char *));
102 - alloced = environ = p;
103 + bcopy(*environp, p, cnt * sizeof(char *));
104 + *environp = p;
105 }
106 - environ[cnt + 1] = NULL;
107 + (*environp)[cnt + 1] = NULL;
108 offset = cnt;
109 }
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))))
113 - return (-1);
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. */
119 + if (copy > 0) {
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))))
123 + return (-1);
124 + for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
125 + for (*c++ = '='; (*c++ = *value++); );
126 + } else {
127 + /* the legacy behavior copies the string */
128 + if (copy < 0) {
129 + size_t len = strlen(name);
130 + if((c = malloc_zone_malloc(envz, len + 1)) == NULL)
131 + return (-1);
132 + memcpy(c, name, len + 1);
133 + name = c;
134 + }
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;
139 + }
140 return (0);
141 }
142
143 +__private_extern__ void
144 +__unsetenv(const char *name, char **environ, malloc_zone_t *envz)
145 +{
146 + char **p;
147 + int offset;
148 +
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)))
155 + break;
156 + }
157 +}
158 +
159 +/****************************************************************************/
160 +/*
161 + * _allocenvstate -- SPI that creates a new state (opaque)
162 + */
163 +void *
164 +_allocenvstate(void)
165 +{
166 + return (void *)malloc_create_zone(1000 /* unused */, 0 /* unused */);
167 +}
168 +
169 +/*
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.)
175 + */
176 +char **
177 +_copyenv(char **env)
178 +{
179 + char **p;
180 + int cnt = 1;
181 +
182 + if (env)
183 + for (p = env; *p; ++p, ++cnt);
184 + p = (char **)malloc((size_t)(sizeof(char *) * cnt));
185 + if (!p)
186 + return (NULL);
187 + if (env)
188 + bcopy(env, p, cnt * sizeof(char *));
189 + else
190 + *p = NULL;
191 + return p;
192 +}
193 +
194 +/*
195 + * _deallocenvstate -- SPI that frees all the memory associated with the state
196 + * and all allocated strings, including the environment array itself if it
197 + * was copied.
198 + */
199 +int
200 +_deallocenvstate(void *state)
201 +{
202 + malloc_zone_t *envz;
203 +
204 + if (!(envz = (malloc_zone_t *)state) || envz == __zone0) {
205 + errno = EINVAL;
206 + return -1;
207 + }
208 + malloc_destroy_zone(envz);
209 + return 0;
210 +}
211 +
212 +/*
213 + * setenvp -- SPI using an arbitrary pointer to string array and an env state,
214 + * created by _allocenvstate(). Initial checking is not done.
215 + *
216 + * Set the value of the environmental variable "name" to be
217 + * "value". If rewrite is set, replace any current value.
218 + */
219 +int
220 +_setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state)
221 +{
222 + /* insure __zone0 is set up */
223 + if (!__zone0) {
224 + __zone0 = malloc_create_zone(0, 0);
225 + if (!__zone0) {
226 + errno = ENOMEM;
227 + return (-1);
228 + }
229 + }
230 + return (__setenv(name, value, rewrite, 1, envp, (state ? (malloc_zone_t *)state : __zone0)));
231 +}
232 +
233 +/*
234 + * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
235 + * state, created by _allocenvstate(). Initial checking is not done.
236 + *
237 + * Delete environmental variable "name".
238 + */
239 +int
240 +_unsetenvp(const char *name, char ***envp, void *state)
241 +{
242 + /* insure __zone0 is set up */
243 + if (!__zone0) {
244 + __zone0 = malloc_create_zone(0, 0);
245 + if (!__zone0) {
246 + errno = ENOMEM;
247 + return (-1);
248 + }
249 + }
250 + __unsetenv(name, *envp, (state ? (malloc_zone_t *)state : __zone0));
251 + return 0;
252 +}
253 +
254 +#endif /* !BUILD_VARIANT */
255 +
256 +/*
257 + * setenv --
258 + * Set the value of the environmental variable "name" to be
259 + * "value". If rewrite is set, replace any current value.
260 + */
261 +int
262 +setenv(name, value, rewrite)
263 + const char *name;
264 + const char *value;
265 + int rewrite;
266 +{
267 + /* no null ptr or empty str */
268 + if(name == NULL || *name == 0) {
269 + errno = EINVAL;
270 + return (-1);
271 + }
272 +
273 +#if __DARWIN_UNIX03
274 + /* no '=' in name */
275 + if (strchr(name, '=')) {
276 + errno = EINVAL;
277 + return (-1);
278 + }
279 +#endif /* __DARWIN_UNIX03 */
280 +
281 + if (*value == '=') /* no `=' in value */
282 + ++value;
283 + /* insure __zone0 is set up before calling __malloc_check_env_name */
284 + if (!__zone0) {
285 + __zone0 = malloc_create_zone(0, 0);
286 + if (!__zone0) {
287 + errno = ENOMEM;
288 + return (-1);
289 + }
290 + }
291 + __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
292 + return (__setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0));
293 +}
294 +
295 /*
296 * unsetenv(name) --
297 * Delete environmental variable "name".
298 */
299 +#if __DARWIN_UNIX03
300 +int
301 +#else /* !__DARWIN_UNIX03 */
302 void
303 +#endif /* __DARWIN_UNIX03 */
304 unsetenv(name)
305 const char *name;
306 {
307 - extern char **environ;
308 - char **p;
309 - int offset;
310 +#if __DARWIN_UNIX03
311 + /* no null ptr or empty str */
312 + if(name == NULL || *name == 0) {
313 + errno = EINVAL;
314 + return (-1);
315 + }
316
317 - while (__findenv(name, &offset)) /* if set multiple times */
318 - for (p = &environ[offset];; ++p)
319 - if (!(*p = *(p + 1)))
320 - break;
321 + /* no '=' in name */
322 + if (strchr(name, '=')) {
323 + errno = EINVAL;
324 + return (-1);
325 + }
326 + /* insure __zone0 is set up before calling __malloc_check_env_name */
327 + if (!__zone0) {
328 + __zone0 = malloc_create_zone(0, 0);
329 + if (!__zone0) {
330 + errno = ENOMEM;
331 + return (-1);
332 + }
333 + }
334 +#else /* !__DARWIN_UNIX03 */
335 + /* no null ptr or empty str */
336 + if(name == NULL || *name == 0)
337 + return;
338 + /* insure __zone0 is set up before calling __malloc_check_env_name */
339 + if (!__zone0) {
340 + __zone0 = malloc_create_zone(0, 0);
341 + if (!__zone0)
342 + return;
343 + }
344 +#endif /* __DARWIN_UNIX03 */
345 + __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
346 + __unsetenv(name, *_NSGetEnviron(), __zone0);
347 +#if __DARWIN_UNIX03
348 + return 0;
349 +#endif /* __DARWIN_UNIX03 */
350 }