]>
Commit | Line | Data |
---|---|---|
224c7076 A |
1 | /* |
2 | * Copyright (c) 1987, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
224c7076 A |
13 | * 4. Neither the name of the University nor the names of its contributors |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | #if defined(LIBC_SCCS) && !defined(lint) | |
31 | static char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93"; | |
32 | #endif /* LIBC_SCCS and not lint */ | |
33 | #include <sys/cdefs.h> | |
1f2f436a | 34 | __FBSDID("$FreeBSD: src/lib/libc/stdlib/setenv.c,v 1.14 2007/05/01 16:02:41 ache Exp $"); |
224c7076 A |
35 | |
36 | #include <stddef.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <crt_externs.h> | |
40 | #include <errno.h> | |
41 | #include <sys/types.h> | |
42 | #include <fcntl.h> | |
43 | #include <malloc/malloc.h> | |
44 | ||
45 | #define ZONE_OWNS_PTR(zone, ptr) (malloc_zone_from_ptr((ptr)) == zone) | |
46 | ||
47 | extern malloc_zone_t *__zone0; | |
48 | extern void __malloc_check_env_name(const char *); | |
49 | ||
50 | __private_extern__ char *__findenv(const char *, int *, char **); | |
51 | __private_extern__ int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t *); | |
52 | __private_extern__ void __unsetenv(const char *, char **, malloc_zone_t *); | |
1f2f436a A |
53 | __private_extern__ int init__zone0(int); |
54 | ||
55 | /* | |
56 | * _cthread_init_routine used to be called from crt1.o to initialize threads. | |
57 | * This is no longer needed, as initialization happens in dylib initializers, | |
58 | * but is provided to maintain backwards compatibility. Normally, for 10.5 | |
59 | * or greater, _cthread_init_routine does nothing. | |
60 | * | |
61 | * Before 10.5, the _start routine in crt1.o clobbers environ with the original | |
62 | * stack value, which effectively undoes any environment changes made in | |
63 | * initializers. When LEGACY_CRT1_ENVIRON is defined, we replace the | |
64 | * do-nothing routine with one that attempts to restore the environ value. | |
65 | * But this only works if the setenv (and family) routines were used | |
66 | * exclusively, (no direct manipulation of environ). Note that according to | |
67 | * SUSv3, direct manipulation of environ may result in undefined behavior in | |
68 | * setenv and family, so we don't support that (on less than 10.5). | |
69 | */ | |
70 | #ifdef BUILDING_VARIANT | |
71 | # ifdef LEGACY_CRT1_ENVIRON | |
72 | extern char **_saved_environ; | |
73 | # endif /* LEGACY_CRT1_ENVIRON */ | |
74 | #else /* !BUILDING_VARIANT */ | |
75 | # ifdef LEGACY_CRT1_ENVIRON | |
76 | __private_extern__ char **_saved_environ = NULL; | |
77 | ||
78 | static int | |
79 | _legacy_crt1_environ(void) | |
80 | { | |
81 | if (_saved_environ) *_NSGetEnviron() = _saved_environ; | |
82 | return 0; | |
83 | } | |
84 | int (*_cthread_init_routine)(void) = _legacy_crt1_environ; | |
85 | ||
86 | # else /* !LEGACY_CRT1_ENVIRON */ | |
87 | static int _do_nothing(void) { return 0; } | |
88 | int (*_cthread_init_routine)(void) = _do_nothing; | |
89 | # endif /* !LEGACY_CRT1_ENVIRON */ | |
224c7076 | 90 | |
34e8f829 A |
91 | /* |
92 | * Create the environment malloc zone and give it a recognizable name. | |
93 | */ | |
94 | __private_extern__ int | |
95 | init__zone0(int should_set_errno) | |
96 | { | |
97 | if (__zone0) return (0); | |
98 | ||
99 | __zone0 = malloc_create_zone(0, 0); | |
100 | if (!__zone0) { | |
101 | if (should_set_errno) { | |
102 | errno = ENOMEM; | |
103 | } | |
104 | return (-1); | |
105 | } | |
106 | malloc_set_zone_name(__zone0, "environ"); | |
107 | return (0); | |
108 | } | |
109 | ||
224c7076 A |
110 | /* |
111 | * The copy flag may have 3 values: | |
112 | * 1 - make a copy of the name/value pair | |
113 | * 0 - take the name as a user-supplied name=value string | |
114 | * -1 - like 0, except we copy of the name=value string in name | |
115 | */ | |
116 | __private_extern__ int | |
117 | __setenv(name, value, rewrite, copy, environp, envz) | |
118 | const char *name; | |
119 | const char *value; | |
120 | int rewrite, copy; | |
121 | char ***environp; | |
122 | malloc_zone_t *envz; | |
123 | { | |
124 | char *c; | |
125 | int offset; | |
126 | ||
127 | if ((c = __findenv(name, &offset, *environp))) { /* find if already exists */ | |
128 | char *e; | |
129 | if (!rewrite) | |
130 | return (0); | |
131 | /* | |
132 | * In UNIX03, we can overwrite only if we allocated the | |
133 | * string. Then we can realloc it if it is too small. | |
134 | */ | |
135 | e = (*environp)[offset]; | |
136 | if (copy > 0 && ZONE_OWNS_PTR(envz, e)) { | |
137 | size_t l_value = strlen(value); | |
138 | if (strlen(c) < l_value) { /* old smaller; resize*/ | |
139 | char *r; | |
140 | size_t len = c - e; | |
141 | if ((r = realloc(e, l_value + len + 1)) == NULL) | |
142 | return (-1); | |
143 | if (r != e) { | |
144 | (*environp)[offset] = r; | |
145 | c = r + len; | |
146 | } | |
147 | } | |
148 | while ( (*c++ = *value++) ); | |
149 | return (0); | |
150 | } | |
151 | } else { /* create new slot */ | |
152 | int cnt; | |
153 | char **p; | |
154 | ||
155 | for (p = *environp, cnt = 0; *p; ++p, ++cnt); | |
156 | if (ZONE_OWNS_PTR(envz, *environp)) { /* just increase size */ | |
157 | p = (char **)realloc((char *)*environp, | |
158 | (size_t)(sizeof(char *) * (cnt + 2))); | |
159 | if (!p) | |
160 | return (-1); | |
161 | *environp = p; | |
162 | } | |
163 | else { /* get new space */ | |
164 | /* copy old entries into it */ | |
165 | p = malloc_zone_malloc(envz, (size_t)(sizeof(char *) * (cnt + 2))); | |
166 | if (!p) | |
167 | return (-1); | |
168 | bcopy(*environp, p, cnt * sizeof(char *)); | |
169 | *environp = p; | |
170 | } | |
171 | (*environp)[cnt + 1] = NULL; | |
172 | offset = cnt; | |
173 | } | |
174 | /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's | |
175 | * strings. For Unix03 putenv(), we put the string directly in | |
176 | * the environment. */ | |
177 | if (copy > 0) { | |
178 | for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */ | |
179 | if (!((*environp)[offset] = /* name + `=' + value */ | |
180 | malloc_zone_malloc(envz, (size_t)((int)(c - name) + strlen(value) + 2)))) | |
181 | return (-1); | |
182 | for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c); | |
183 | for (*c++ = '='; (*c++ = *value++); ); | |
184 | } else { | |
185 | /* the legacy behavior copies the string */ | |
186 | if (copy < 0) { | |
187 | size_t len = strlen(name); | |
188 | if((c = malloc_zone_malloc(envz, len + 1)) == NULL) | |
189 | return (-1); | |
190 | memcpy(c, name, len + 1); | |
191 | name = c; | |
192 | } | |
193 | /* if we malloc-ed the previous value, free it first */ | |
194 | if ((*environp)[offset] != NULL && ZONE_OWNS_PTR(envz, (*environp)[offset])) | |
195 | free((*environp)[offset]); | |
196 | (*environp)[offset] = (char *)name; | |
197 | } | |
198 | return (0); | |
199 | } | |
200 | ||
201 | __private_extern__ void | |
202 | __unsetenv(const char *name, char **environ, malloc_zone_t *envz) | |
203 | { | |
204 | char **p; | |
205 | int offset; | |
206 | ||
207 | while (__findenv(name, &offset, environ)) { /* if set multiple times */ | |
208 | /* if we malloc-ed it, free it first */ | |
209 | if (ZONE_OWNS_PTR(envz, environ[offset])) | |
210 | free(environ[offset]); | |
211 | for (p = &environ[offset];; ++p) | |
212 | if (!(*p = *(p + 1))) | |
213 | break; | |
214 | } | |
215 | } | |
216 | ||
217 | /****************************************************************************/ | |
218 | /* | |
219 | * _allocenvstate -- SPI that creates a new state (opaque) | |
220 | */ | |
221 | void * | |
222 | _allocenvstate(void) | |
223 | { | |
34e8f829 A |
224 | malloc_zone_t *zone; |
225 | zone = malloc_create_zone(1000 /* unused */, 0 /* unused */); | |
226 | if (zone) { | |
227 | malloc_set_zone_name(zone, "environ"); | |
228 | } | |
229 | return (void *)zone; | |
224c7076 A |
230 | } |
231 | ||
232 | /* | |
233 | * _copyenv -- SPI that copies a NULL-tereminated char * array in a newly | |
234 | * allocated buffer, compatible with the other SPI env routines. If env | |
235 | * is NULL, a char * array composed of a single NULL is returned. NULL | |
236 | * is returned on error. (This isn't needed anymore, as __setenv will | |
237 | * automatically make a copy in the zone.) | |
238 | */ | |
239 | char ** | |
240 | _copyenv(char **env) | |
241 | { | |
242 | char **p; | |
243 | int cnt = 1; | |
244 | ||
245 | if (env) | |
246 | for (p = env; *p; ++p, ++cnt); | |
247 | p = (char **)malloc((size_t)(sizeof(char *) * cnt)); | |
248 | if (!p) | |
249 | return (NULL); | |
250 | if (env) | |
251 | bcopy(env, p, cnt * sizeof(char *)); | |
252 | else | |
253 | *p = NULL; | |
254 | return p; | |
255 | } | |
256 | ||
257 | /* | |
258 | * _deallocenvstate -- SPI that frees all the memory associated with the state | |
259 | * and all allocated strings, including the environment array itself if it | |
260 | * was copied. | |
261 | */ | |
262 | int | |
263 | _deallocenvstate(void *state) | |
264 | { | |
265 | malloc_zone_t *envz; | |
266 | ||
267 | if (!(envz = (malloc_zone_t *)state) || envz == __zone0) { | |
268 | errno = EINVAL; | |
269 | return -1; | |
270 | } | |
271 | malloc_destroy_zone(envz); | |
272 | return 0; | |
273 | } | |
274 | ||
275 | /* | |
276 | * setenvp -- SPI using an arbitrary pointer to string array and an env state, | |
277 | * created by _allocenvstate(). Initial checking is not done. | |
278 | * | |
279 | * Set the value of the environmental variable "name" to be | |
280 | * "value". If rewrite is set, replace any current value. | |
281 | */ | |
282 | int | |
283 | _setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state) | |
284 | { | |
34e8f829 | 285 | if (init__zone0(1)) return (-1); |
224c7076 A |
286 | return (__setenv(name, value, rewrite, 1, envp, (state ? (malloc_zone_t *)state : __zone0))); |
287 | } | |
288 | ||
289 | /* | |
290 | * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env | |
291 | * state, created by _allocenvstate(). Initial checking is not done. | |
292 | * | |
293 | * Delete environmental variable "name". | |
294 | */ | |
295 | int | |
296 | _unsetenvp(const char *name, char ***envp, void *state) | |
297 | { | |
34e8f829 | 298 | if (init__zone0(1)) return (-1); |
224c7076 A |
299 | __unsetenv(name, *envp, (state ? (malloc_zone_t *)state : __zone0)); |
300 | return 0; | |
301 | } | |
302 | ||
303 | #endif /* !BUILD_VARIANT */ | |
304 | ||
305 | /* | |
306 | * setenv -- | |
307 | * Set the value of the environmental variable "name" to be | |
308 | * "value". If rewrite is set, replace any current value. | |
309 | */ | |
310 | int | |
311 | setenv(name, value, rewrite) | |
312 | const char *name; | |
313 | const char *value; | |
314 | int rewrite; | |
315 | { | |
1f2f436a A |
316 | #ifdef LEGACY_CRT1_ENVIRON |
317 | int ret; | |
318 | #endif /* LEGACY_CRT1_ENVIRON */ | |
319 | ||
224c7076 A |
320 | /* no null ptr or empty str */ |
321 | if(name == NULL || *name == 0) { | |
322 | errno = EINVAL; | |
323 | return (-1); | |
324 | } | |
325 | ||
326 | #if __DARWIN_UNIX03 | |
327 | /* no '=' in name */ | |
328 | if (strchr(name, '=')) { | |
329 | errno = EINVAL; | |
330 | return (-1); | |
331 | } | |
332 | #endif /* __DARWIN_UNIX03 */ | |
333 | ||
334 | if (*value == '=') /* no `=' in value */ | |
335 | ++value; | |
336 | /* insure __zone0 is set up before calling __malloc_check_env_name */ | |
34e8f829 | 337 | if (init__zone0(1)) return (-1); |
224c7076 | 338 | __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */ |
1f2f436a A |
339 | #ifdef LEGACY_CRT1_ENVIRON |
340 | ret = __setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0); | |
341 | _saved_environ = *_NSGetEnviron(); | |
342 | return ret; | |
343 | #else /* !LEGACY_CRT1_ENVIRON */ | |
224c7076 | 344 | return (__setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0)); |
1f2f436a | 345 | #endif /* !LEGACY_CRT1_ENVIRON */ |
224c7076 A |
346 | } |
347 | ||
348 | /* | |
349 | * unsetenv(name) -- | |
350 | * Delete environmental variable "name". | |
351 | */ | |
352 | #if __DARWIN_UNIX03 | |
353 | int | |
354 | #else /* !__DARWIN_UNIX03 */ | |
355 | void | |
356 | #endif /* __DARWIN_UNIX03 */ | |
357 | unsetenv(name) | |
358 | const char *name; | |
359 | { | |
360 | #if __DARWIN_UNIX03 | |
361 | /* no null ptr or empty str */ | |
362 | if(name == NULL || *name == 0) { | |
363 | errno = EINVAL; | |
364 | return (-1); | |
365 | } | |
366 | ||
367 | /* no '=' in name */ | |
368 | if (strchr(name, '=')) { | |
369 | errno = EINVAL; | |
370 | return (-1); | |
371 | } | |
372 | /* insure __zone0 is set up before calling __malloc_check_env_name */ | |
34e8f829 | 373 | if (init__zone0(1)) return (-1); |
224c7076 A |
374 | #else /* !__DARWIN_UNIX03 */ |
375 | /* no null ptr or empty str */ | |
376 | if(name == NULL || *name == 0) | |
377 | return; | |
378 | /* insure __zone0 is set up before calling __malloc_check_env_name */ | |
34e8f829 | 379 | if (init__zone0(0)) return; |
224c7076 A |
380 | #endif /* __DARWIN_UNIX03 */ |
381 | __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */ | |
382 | __unsetenv(name, *_NSGetEnviron(), __zone0); | |
383 | #if __DARWIN_UNIX03 | |
384 | return 0; | |
385 | #endif /* __DARWIN_UNIX03 */ | |
386 | } |