]>
git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/setenv.c
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>
45 extern char *__findenv_locked(const char *, int *, char **);
46 extern int __setenv_locked(const char *, const char *, int, int, char ***, struct owned_ptr
*);
47 extern void __unsetenv_locked(const char *, char **, struct owned_ptr
*);
49 extern void __environ_lock(void);
50 extern void __environ_unlock(void);
52 extern struct owned_ptr
*__env_owned
;
53 extern int __init__env_owned_locked(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 */
91 __private_extern__
struct owned_ptr
*__env_owned
= NULL
;
94 * The owned_ptr structure is a table of pointers that we own, and can
95 * realloc/free as we choose. table[0] is always NULL, so it is always
96 * less that any real pointer. "used" is the number of table entries in use
97 * (including the initial NULL), while "size" is the allocated size of the
98 * table (used to enlarge the size of the table when needed).
106 #define OWNED_PTR_INITIAL_SIZE 8
108 __private_extern__
int _owned_ptr_search(struct owned_ptr
* __restrict
, const void * __restrict
, int * __restrict
);
110 __private_extern__
void
111 _owned_ptr_add(struct owned_ptr
* __restrict owned
, const void * __restrict ptr
)
115 if (_owned_ptr_search(owned
, ptr
, &index
) == 0) return; /* already there */
116 if (owned
->used
>= owned
->size
) {
117 int new_size
= 2 * owned
->size
;
118 const void **new_table
= (const void **)realloc(owned
->table
,
119 new_size
* sizeof(const void *));
121 /* no memory to enlarge the table, so just drop */
124 owned
->table
= new_table
;
125 owned
->size
= new_size
;
127 memmove(owned
->table
+ index
+ 2, owned
->table
+ index
+ 1,
128 sizeof(void *) * (owned
->used
- index
- 1));
129 owned
->table
[index
+ 1] = ptr
;
133 __private_extern__
struct owned_ptr
*
134 _owned_ptr_alloc(void)
136 struct owned_ptr
*owned
;
138 owned
= (struct owned_ptr
*)malloc(sizeof(struct owned_ptr
));
139 if (!owned
) return NULL
;
140 owned
->table
= (const void **)malloc(OWNED_PTR_INITIAL_SIZE
*
141 sizeof(const void *));
148 owned
->table
[0] = NULL
;
150 owned
->size
= OWNED_PTR_INITIAL_SIZE
;
154 __private_extern__
void
155 _owned_ptr_delete(struct owned_ptr
*owned
, int index
)
157 if (!index
|| index
>= owned
->used
) return;
158 memmove(owned
->table
+ index
, owned
->table
+ index
+ 1,
159 sizeof(void *) * (owned
->used
- index
- 1));
163 __private_extern__
void
164 _owned_ptr_free(struct owned_ptr
*owned
)
171 * Search owned->table for "ptr". Zero is returned if found, non-zero means
172 * not found. If "result" is non-NULL, the found index is returned in it, or
173 * if not found, the index of the immediate lower value (ptr can be inserted
176 __private_extern__
int
177 _owned_ptr_search(struct owned_ptr
* __restrict owned
, const void * __restrict ptr
, int * __restrict result
)
180 int high
= owned
->used
- 1;
183 if (owned
->table
[high
] < ptr
) {
184 if (result
) *result
= high
;
186 } else if (owned
->table
[high
] == ptr
) {
187 if (result
) *result
= high
;
190 while (high
- low
> 1) {
191 cur
= (low
+ high
) / 2;
192 if (ptr
> owned
->table
[cur
]) {
194 } else if (ptr
< owned
->table
[cur
]) {
198 if (result
) *result
= cur
;
202 /* no match found; *result will be the insert-after position */
203 if (result
) *result
= low
;
208 * Initialize the process's __env_owned structure
210 __private_extern__
int
211 __init__env_owned_locked(int should_set_errno
)
215 if (__env_owned
) return 0;
217 if (!should_set_errno
)
219 __env_owned
= _owned_ptr_alloc();
221 if (!should_set_errno
)
229 * The copy flag may have 3 values:
230 * 1 - make a copy of the name/value pair
231 * 0 - take the name as a user-supplied name=value string
232 * -1 - like 0, except we copy of the name=value string in name
234 __private_extern__
int
235 __setenv_locked(name
, value
, rewrite
, copy
, environp
, owned
)
240 struct owned_ptr
*owned
;
246 if ((c
= __findenv_locked(name
, &offset
, *environp
))) { /* find if already exists */
251 * In UNIX03, we can overwrite only if we allocated the
252 * string. Then we can realloc it if it is too small.
254 e
= (*environp
)[offset
];
255 if (_owned_ptr_search(owned
, e
, &oindex
) == 0) {
257 size_t l_value
= strlen(value
);
258 if (strlen(c
) < l_value
) { /* old smaller; resize*/
261 if ((r
= realloc(e
, l_value
+ len
+ 1)) == NULL
)
264 (*environp
)[offset
] = r
;
266 _owned_ptr_delete(owned
, oindex
);
267 _owned_ptr_add(owned
, r
);
270 while ( (*c
++ = *value
++) );
273 /* Free up the slot for later use (putenv) */
274 _owned_ptr_delete(owned
, oindex
);
277 } else { /* create new slot */
281 for (p
= *environp
, cnt
= 0; *p
; ++p
, ++cnt
);
282 if (_owned_ptr_search(owned
, *environp
, &oindex
) == 0) { /* just increase size */
283 p
= (char **)realloc((char *)*environp
,
284 (size_t)(sizeof(char *) * (cnt
+ 2)));
287 if (*environp
!= p
) {
288 _owned_ptr_delete(owned
, oindex
);
289 _owned_ptr_add(owned
, p
);
293 else { /* get new space */
294 /* copy old entries into it */
295 p
= malloc((size_t)(sizeof(char *) * (cnt
+ 2)));
298 _owned_ptr_add(owned
, p
);
299 bcopy(*environp
, p
, cnt
* sizeof(char *));
302 (*environp
)[cnt
+ 1] = NULL
;
305 /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
306 * strings. For Unix03 putenv(), we put the string directly in
307 * the environment. */
309 for (c
= (char *)name
; *c
&& *c
!= '='; ++c
); /* no `=' in name */
310 if (!((*environp
)[offset
] = /* name + `=' + value */
311 malloc((size_t)((int)(c
- name
) + strlen(value
) + 2))))
313 _owned_ptr_add(owned
, (*environp
)[offset
]);
314 for (c
= (*environp
)[offset
]; (*c
= *name
++) && *c
!= '='; ++c
);
315 for (*c
++ = '='; (*c
++ = *value
++); );
317 /* the legacy behavior copies the string */
319 size_t len
= strlen(name
);
320 if((c
= malloc(len
+ 1)) == NULL
)
322 _owned_ptr_add(owned
, c
);
323 memcpy(c
, name
, len
+ 1);
326 (*environp
)[offset
] = (char *)name
;
331 __private_extern__
void
332 __unsetenv_locked(const char *name
, char **environ
, struct owned_ptr
*owned
)
338 while (__findenv_locked(name
, &offset
, environ
)) { /* if set multiple times */
339 /* if we malloc-ed it, free it first */
340 if (_owned_ptr_search(owned
, environ
[offset
], &oindex
) == 0) {
341 _owned_ptr_delete(owned
, oindex
);
342 free(environ
[offset
]);
344 for (p
= &environ
[offset
];; ++p
)
345 if (!(*p
= *(p
+ 1)))
350 /****************************************************************************/
352 * _allocenvstate -- SPI that creates a new state (opaque)
357 return _owned_ptr_alloc();
361 * _copyenv -- SPI that copies a NULL-tereminated char * array in a newly
362 * allocated buffer, compatible with the other SPI env routines. If env
363 * is NULL, a char * array composed of a single NULL is returned. NULL
364 * is returned on error. (This isn't needed anymore, as __setenv_locked will
365 * automatically make a copy.)
374 for (p
= env
; *p
; ++p
, ++cnt
);
375 p
= (char **)malloc((size_t)(sizeof(char *) * cnt
));
379 bcopy(env
, p
, cnt
* sizeof(char *));
386 * _deallocenvstate -- SPI that frees all the memory associated with the state
387 * and all allocated strings, including the environment array itself if it
391 _deallocenvstate(void *state
)
393 struct owned_ptr
*owned
;
395 if (!(owned
= (struct owned_ptr
*)state
) || owned
== __env_owned
) {
399 _owned_ptr_free(owned
);
404 * setenvp -- SPI using an arbitrary pointer to string array and an env state,
405 * created by _allocenvstate(). Initial checking is not done.
407 * Set the value of the environmental variable "name" to be
408 * "value". If rewrite is set, replace any current value.
411 _setenvp(const char *name
, const char *value
, int rewrite
, char ***envp
, void *state
)
414 if (__init__env_owned_locked(1)) {
418 int ret
= __setenv_locked(name
, value
, rewrite
, 1, envp
,
419 (state
? (struct owned_ptr
*)state
: __env_owned
));
425 * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
426 * state, created by _allocenvstate(). Initial checking is not done.
428 * Delete environmental variable "name".
431 _unsetenvp(const char *name
, char ***envp
, void *state
)
434 if (__init__env_owned_locked(1)) {
438 __unsetenv_locked(name
, *envp
, (state
? (struct owned_ptr
*)state
: __env_owned
));
443 #endif /* !BUILD_VARIANT */
447 * Set the value of the environmental variable "name" to be
448 * "value". If rewrite is set, replace any current value.
451 setenv(name
, value
, rewrite
)
458 /* no null ptr or empty str */
459 if(name
== NULL
|| *name
== 0) {
466 if (strchr(name
, '=')) {
470 #endif /* __DARWIN_UNIX03 */
473 if (__init__env_owned_locked(1)) {
477 ret
= __setenv_locked(name
, value
, rewrite
, 1, _NSGetEnviron(), __env_owned
);
478 #ifdef LEGACY_CRT1_ENVIRON
479 _saved_environ
= *_NSGetEnviron();
480 #endif /* !LEGACY_CRT1_ENVIRON */
486 static inline __attribute__((always_inline
)) int
487 _unsetenv(const char *name
, int should_set_errno
)
490 if (__init__env_owned_locked(should_set_errno
)) {
494 __unsetenv_locked(name
, *_NSGetEnviron(), __env_owned
);
501 * Delete environmental variable "name".
505 unsetenv(const char *name
)
507 /* no null ptr or empty str */
508 if(name
== NULL
|| *name
== 0) {
514 if (strchr(name
, '=')) {
518 return _unsetenv(name
, 1);
520 #else /* !__DARWIN_UNIX03 */
522 unsetenv(const char *name
)
524 /* no null ptr or empty str */
525 if(name
== NULL
|| *name
== 0)
529 #endif /* __DARWIN_UNIX03 */