]>
git.saurik.com Git - apple/libc.git/blob - stdlib/setenv-fbsd.c
8965a6a2f2f5064499a20886e4c1ff0a6ba395c0
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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
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>
34 __FBSDID("$FreeBSD: src/lib/libc/stdlib/setenv.c,v 1.14 2007/05/01 16:02:41 ache Exp $");
39 #include <crt_externs.h>
41 #include <sys/types.h>
43 #include <malloc/malloc.h>
45 #define ZONE_OWNS_PTR(zone, ptr) (malloc_zone_from_ptr((ptr)) == zone)
47 extern malloc_zone_t
*__zone0
;
48 extern void __malloc_check_env_name(const char *);
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
*);
53 __private_extern__
int init__zone0(int);
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.
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).
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
;
79 _legacy_crt1_environ(void)
81 if (_saved_environ
) *_NSGetEnviron() = _saved_environ
;
84 int (*_cthread_init_routine
)(void) = _legacy_crt1_environ
;
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 */
92 * Create the environment malloc zone and give it a recognizable name.
94 __private_extern__
int
95 init__zone0(int should_set_errno
)
97 if (__zone0
) return (0);
99 __zone0
= malloc_create_zone(0, 0);
101 if (should_set_errno
) {
106 malloc_set_zone_name(__zone0
, "environ");
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
116 __private_extern__
int
117 __setenv(name
, value
, rewrite
, copy
, environp
, envz
)
127 if ((c
= __findenv(name
, &offset
, *environp
))) { /* find if already exists */
132 * In UNIX03, we can overwrite only if we allocated the
133 * string. Then we can realloc it if it is too small.
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*/
141 if ((r
= realloc(e
, l_value
+ len
+ 1)) == NULL
)
144 (*environp
)[offset
] = r
;
148 while ( (*c
++ = *value
++) );
151 } else { /* create new slot */
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)));
163 else { /* get new space */
164 /* copy old entries into it */
165 p
= malloc_zone_malloc(envz
, (size_t)(sizeof(char *) * (cnt
+ 2)));
168 bcopy(*environp
, p
, cnt
* sizeof(char *));
171 (*environp
)[cnt
+ 1] = NULL
;
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. */
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))))
182 for (c
= (*environp
)[offset
]; (*c
= *name
++) && *c
!= '='; ++c
);
183 for (*c
++ = '='; (*c
++ = *value
++); );
185 /* the legacy behavior copies the string */
187 size_t len
= strlen(name
);
188 if((c
= malloc_zone_malloc(envz
, len
+ 1)) == NULL
)
190 memcpy(c
, name
, len
+ 1);
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
;
201 __private_extern__
void
202 __unsetenv(const char *name
, char **environ
, malloc_zone_t
*envz
)
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)))
217 /****************************************************************************/
219 * _allocenvstate -- SPI that creates a new state (opaque)
225 zone
= malloc_create_zone(1000 /* unused */, 0 /* unused */);
227 malloc_set_zone_name(zone
, "environ");
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.)
246 for (p
= env
; *p
; ++p
, ++cnt
);
247 p
= (char **)malloc((size_t)(sizeof(char *) * cnt
));
251 bcopy(env
, p
, cnt
* sizeof(char *));
258 * _deallocenvstate -- SPI that frees all the memory associated with the state
259 * and all allocated strings, including the environment array itself if it
263 _deallocenvstate(void *state
)
267 if (!(envz
= (malloc_zone_t
*)state
) || envz
== __zone0
) {
271 malloc_destroy_zone(envz
);
276 * setenvp -- SPI using an arbitrary pointer to string array and an env state,
277 * created by _allocenvstate(). Initial checking is not done.
279 * Set the value of the environmental variable "name" to be
280 * "value". If rewrite is set, replace any current value.
283 _setenvp(const char *name
, const char *value
, int rewrite
, char ***envp
, void *state
)
285 if (init__zone0(1)) return (-1);
286 return (__setenv(name
, value
, rewrite
, 1, envp
, (state
? (malloc_zone_t
*)state
: __zone0
)));
290 * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
291 * state, created by _allocenvstate(). Initial checking is not done.
293 * Delete environmental variable "name".
296 _unsetenvp(const char *name
, char ***envp
, void *state
)
298 if (init__zone0(1)) return (-1);
299 __unsetenv(name
, *envp
, (state
? (malloc_zone_t
*)state
: __zone0
));
303 #endif /* !BUILD_VARIANT */
307 * Set the value of the environmental variable "name" to be
308 * "value". If rewrite is set, replace any current value.
311 setenv(name
, value
, rewrite
)
316 #ifdef LEGACY_CRT1_ENVIRON
318 #endif /* LEGACY_CRT1_ENVIRON */
320 /* no null ptr or empty str */
321 if(name
== NULL
|| *name
== 0) {
328 if (strchr(name
, '=')) {
332 #endif /* __DARWIN_UNIX03 */
334 if (*value
== '=') /* no `=' in value */
336 /* insure __zone0 is set up before calling __malloc_check_env_name */
337 if (init__zone0(1)) return (-1);
338 __malloc_check_env_name(name
); /* see if we are changing a malloc environment variable */
339 #ifdef LEGACY_CRT1_ENVIRON
340 ret
= __setenv(name
, value
, rewrite
, 1, _NSGetEnviron(), __zone0
);
341 _saved_environ
= *_NSGetEnviron();
343 #else /* !LEGACY_CRT1_ENVIRON */
344 return (__setenv(name
, value
, rewrite
, 1, _NSGetEnviron(), __zone0
));
345 #endif /* !LEGACY_CRT1_ENVIRON */
350 * Delete environmental variable "name".
354 #else /* !__DARWIN_UNIX03 */
356 #endif /* __DARWIN_UNIX03 */
361 /* no null ptr or empty str */
362 if(name
== NULL
|| *name
== 0) {
368 if (strchr(name
, '=')) {
372 /* insure __zone0 is set up before calling __malloc_check_env_name */
373 if (init__zone0(1)) return (-1);
374 #else /* !__DARWIN_UNIX03 */
375 /* no null ptr or empty str */
376 if(name
== NULL
|| *name
== 0)
378 /* insure __zone0 is set up before calling __malloc_check_env_name */
379 if (init__zone0(0)) return;
380 #endif /* __DARWIN_UNIX03 */
381 __malloc_check_env_name(name
); /* see if we are changing a malloc environment variable */
382 __unsetenv(name
, *_NSGetEnviron(), __zone0
);
385 #endif /* __DARWIN_UNIX03 */