]>
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 __private_extern__
char *__findenv(const char *, int *, char **);
46 __private_extern__
int __setenv(const char *, const char *, int, int, char ***, struct owned_ptr
*);
47 __private_extern__
void __unsetenv(const char *, char **, struct owned_ptr
*);
49 __private_extern__
struct owned_ptr
*__env_owned
;
50 __private_extern__
int __init__env_owned(int);
53 * _cthread_init_routine used to be called from crt1.o to initialize threads.
54 * This is no longer needed, as initialization happens in dylib initializers,
55 * but is provided to maintain backwards compatibility. Normally, for 10.5
56 * or greater, _cthread_init_routine does nothing.
58 * Before 10.5, the _start routine in crt1.o clobbers environ with the original
59 * stack value, which effectively undoes any environment changes made in
60 * initializers. When LEGACY_CRT1_ENVIRON is defined, we replace the
61 * do-nothing routine with one that attempts to restore the environ value.
62 * But this only works if the setenv (and family) routines were used
63 * exclusively, (no direct manipulation of environ). Note that according to
64 * SUSv3, direct manipulation of environ may result in undefined behavior in
65 * setenv and family, so we don't support that (on less than 10.5).
67 #ifdef BUILDING_VARIANT
68 # ifdef LEGACY_CRT1_ENVIRON
69 extern char **_saved_environ
;
70 # endif /* LEGACY_CRT1_ENVIRON */
71 #else /* !BUILDING_VARIANT */
72 # ifdef LEGACY_CRT1_ENVIRON
73 __private_extern__
char **_saved_environ
= NULL
;
76 _legacy_crt1_environ(void)
78 if (_saved_environ
) *_NSGetEnviron() = _saved_environ
;
81 int (*_cthread_init_routine
)(void) = _legacy_crt1_environ
;
83 # else /* !LEGACY_CRT1_ENVIRON */
84 static int _do_nothing(void) { return 0; }
85 int (*_cthread_init_routine
)(void) = _do_nothing
;
86 # endif /* !LEGACY_CRT1_ENVIRON */
88 __private_extern__
struct owned_ptr
*__env_owned
= NULL
;
91 * The owned_ptr structure is a table of pointers that we own, and can
92 * realloc/free as we choose. table[0] is always NULL, so it is always
93 * less that any real pointer. "used" is the number of table entries in use
94 * (including the initial NULL), while "size" is the allocated size of the
95 * table (used to enlarge the size of the table when needed).
103 #define OWNED_PTR_INITIAL_SIZE 8
105 __private_extern__
int _owned_ptr_search(struct owned_ptr
* __restrict
, const void * __restrict
, int * __restrict
);
107 __private_extern__
void
108 _owned_ptr_add(struct owned_ptr
* __restrict owned
, const void * __restrict ptr
)
112 if (_owned_ptr_search(owned
, ptr
, &index
) == 0) return; /* already there */
113 if (owned
->used
>= owned
->size
) {
114 int new_size
= 2 * owned
->size
;
115 const void **new_table
= (const void **)realloc(owned
->table
,
116 new_size
* sizeof(const void *));
118 /* no memory to enlarge the table, so just drop */
121 owned
->table
= new_table
;
122 owned
->size
= new_size
;
124 memmove(owned
->table
+ index
+ 2, owned
->table
+ index
+ 1,
125 sizeof(void *) * (owned
->used
- index
- 1));
126 owned
->table
[index
+ 1] = ptr
;
130 __private_extern__
struct owned_ptr
*
131 _owned_ptr_alloc(void)
133 struct owned_ptr
*owned
;
135 owned
= (struct owned_ptr
*)malloc(sizeof(struct owned_ptr
));
136 if (!owned
) return NULL
;
137 owned
->table
= (const void **)malloc(OWNED_PTR_INITIAL_SIZE
*
138 sizeof(const void *));
145 owned
->table
[0] = NULL
;
147 owned
->size
= OWNED_PTR_INITIAL_SIZE
;
151 __private_extern__
void
152 _owned_ptr_delete(struct owned_ptr
*owned
, int index
)
154 if (!index
|| index
>= owned
->used
) return;
155 memmove(owned
->table
+ index
, owned
->table
+ index
+ 1,
156 sizeof(void *) * (owned
->used
- index
- 1));
160 __private_extern__
void
161 _owned_ptr_free(struct owned_ptr
*owned
)
168 * Search owned->table for "ptr". Zero is returned if found, non-zero means
169 * not found. If "result" is non-NULL, the found index is returned in it, or
170 * if not found, the index of the immediate lower value (ptr can be inserted
173 __private_extern__
int
174 _owned_ptr_search(struct owned_ptr
* __restrict owned
, const void * __restrict ptr
, int * __restrict result
)
177 int high
= owned
->used
- 1;
180 if (owned
->table
[high
] < ptr
) {
181 if (result
) *result
= high
;
183 } else if (owned
->table
[high
] == ptr
) {
184 if (result
) *result
= high
;
187 while (high
- low
> 1) {
188 cur
= (low
+ high
) / 2;
189 if (ptr
> owned
->table
[cur
]) {
191 } else if (ptr
< owned
->table
[cur
]) {
195 if (result
) *result
= cur
;
199 /* no match found; *result will be the insert-after position */
200 if (result
) *result
= low
;
205 * Initialize the process's __env_owned structure
207 __private_extern__
int
208 __init__env_owned(int should_set_errno
)
212 if (__env_owned
) return 0;
214 if (!should_set_errno
)
216 __env_owned
= _owned_ptr_alloc();
218 if (!should_set_errno
)
226 * The copy flag may have 3 values:
227 * 1 - make a copy of the name/value pair
228 * 0 - take the name as a user-supplied name=value string
229 * -1 - like 0, except we copy of the name=value string in name
231 __private_extern__
int
232 __setenv(name
, value
, rewrite
, copy
, environp
, owned
)
237 struct owned_ptr
*owned
;
243 if ((c
= __findenv(name
, &offset
, *environp
))) { /* find if already exists */
248 * In UNIX03, we can overwrite only if we allocated the
249 * string. Then we can realloc it if it is too small.
251 e
= (*environp
)[offset
];
252 if (_owned_ptr_search(owned
, e
, &oindex
) == 0) {
254 size_t l_value
= strlen(value
);
255 if (strlen(c
) < l_value
) { /* old smaller; resize*/
258 if ((r
= realloc(e
, l_value
+ len
+ 1)) == NULL
)
261 (*environp
)[offset
] = r
;
263 _owned_ptr_delete(owned
, oindex
);
264 _owned_ptr_add(owned
, r
);
267 while ( (*c
++ = *value
++) );
270 /* Free up the slot for later use (putenv) */
271 _owned_ptr_delete(owned
, oindex
);
274 } else { /* create new slot */
278 for (p
= *environp
, cnt
= 0; *p
; ++p
, ++cnt
);
279 if (_owned_ptr_search(owned
, *environp
, &oindex
) == 0) { /* just increase size */
280 p
= (char **)realloc((char *)*environp
,
281 (size_t)(sizeof(char *) * (cnt
+ 2)));
284 if (*environp
!= p
) {
285 _owned_ptr_delete(owned
, oindex
);
286 _owned_ptr_add(owned
, p
);
290 else { /* get new space */
291 /* copy old entries into it */
292 p
= malloc((size_t)(sizeof(char *) * (cnt
+ 2)));
295 _owned_ptr_add(owned
, p
);
296 bcopy(*environp
, p
, cnt
* sizeof(char *));
299 (*environp
)[cnt
+ 1] = NULL
;
302 /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
303 * strings. For Unix03 putenv(), we put the string directly in
304 * the environment. */
306 for (c
= (char *)name
; *c
&& *c
!= '='; ++c
); /* no `=' in name */
307 if (!((*environp
)[offset
] = /* name + `=' + value */
308 malloc((size_t)((int)(c
- name
) + strlen(value
) + 2))))
310 _owned_ptr_add(owned
, (*environp
)[offset
]);
311 for (c
= (*environp
)[offset
]; (*c
= *name
++) && *c
!= '='; ++c
);
312 for (*c
++ = '='; (*c
++ = *value
++); );
314 /* the legacy behavior copies the string */
316 size_t len
= strlen(name
);
317 if((c
= malloc(len
+ 1)) == NULL
)
319 _owned_ptr_add(owned
, c
);
320 memcpy(c
, name
, len
+ 1);
323 (*environp
)[offset
] = (char *)name
;
328 __private_extern__
void
329 __unsetenv(const char *name
, char **environ
, struct owned_ptr
*owned
)
335 while (__findenv(name
, &offset
, environ
)) { /* if set multiple times */
336 /* if we malloc-ed it, free it first */
337 if (_owned_ptr_search(owned
, environ
[offset
], &oindex
) == 0) {
338 _owned_ptr_delete(owned
, oindex
);
339 free(environ
[offset
]);
341 for (p
= &environ
[offset
];; ++p
)
342 if (!(*p
= *(p
+ 1)))
347 /****************************************************************************/
349 * _allocenvstate -- SPI that creates a new state (opaque)
354 return _owned_ptr_alloc();
358 * _copyenv -- SPI that copies a NULL-tereminated char * array in a newly
359 * allocated buffer, compatible with the other SPI env routines. If env
360 * is NULL, a char * array composed of a single NULL is returned. NULL
361 * is returned on error. (This isn't needed anymore, as __setenv will
362 * automatically make a copy.)
371 for (p
= env
; *p
; ++p
, ++cnt
);
372 p
= (char **)malloc((size_t)(sizeof(char *) * cnt
));
376 bcopy(env
, p
, cnt
* sizeof(char *));
383 * _deallocenvstate -- SPI that frees all the memory associated with the state
384 * and all allocated strings, including the environment array itself if it
388 _deallocenvstate(void *state
)
390 struct owned_ptr
*owned
;
392 if (!(owned
= (struct owned_ptr
*)state
) || owned
== __env_owned
) {
396 _owned_ptr_free(owned
);
401 * setenvp -- SPI using an arbitrary pointer to string array and an env state,
402 * created by _allocenvstate(). Initial checking is not done.
404 * Set the value of the environmental variable "name" to be
405 * "value". If rewrite is set, replace any current value.
408 _setenvp(const char *name
, const char *value
, int rewrite
, char ***envp
, void *state
)
410 if (__init__env_owned(1)) return (-1);
411 return (__setenv(name
, value
, rewrite
, 1, envp
, (state
? (struct owned_ptr
*)state
: __env_owned
)));
415 * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
416 * state, created by _allocenvstate(). Initial checking is not done.
418 * Delete environmental variable "name".
421 _unsetenvp(const char *name
, char ***envp
, void *state
)
423 if (__init__env_owned(1)) return (-1);
424 __unsetenv(name
, *envp
, (state
? (struct owned_ptr
*)state
: __env_owned
));
428 #endif /* !BUILD_VARIANT */
432 * Set the value of the environmental variable "name" to be
433 * "value". If rewrite is set, replace any current value.
436 setenv(name
, value
, rewrite
)
441 #ifdef LEGACY_CRT1_ENVIRON
443 #endif /* LEGACY_CRT1_ENVIRON */
445 /* no null ptr or empty str */
446 if(name
== NULL
|| *name
== 0) {
453 if (strchr(name
, '=')) {
457 #endif /* __DARWIN_UNIX03 */
459 if (*value
== '=') /* no `=' in value */
461 if (__init__env_owned(1)) return (-1);
462 #ifdef LEGACY_CRT1_ENVIRON
463 ret
= __setenv(name
, value
, rewrite
, 1, _NSGetEnviron(), __env_owned
);
464 _saved_environ
= *_NSGetEnviron();
466 #else /* !LEGACY_CRT1_ENVIRON */
467 return (__setenv(name
, value
, rewrite
, 1, _NSGetEnviron(), __env_owned
));
468 #endif /* !LEGACY_CRT1_ENVIRON */
473 * Delete environmental variable "name".
477 #else /* !__DARWIN_UNIX03 */
479 #endif /* __DARWIN_UNIX03 */
484 /* no null ptr or empty str */
485 if(name
== NULL
|| *name
== 0) {
491 if (strchr(name
, '=')) {
495 if (__init__env_owned(1)) return (-1);
496 #else /* !__DARWIN_UNIX03 */
497 /* no null ptr or empty str */
498 if(name
== NULL
|| *name
== 0)
500 if (__init__env_owned(0)) return;
501 #endif /* __DARWIN_UNIX03 */
502 __unsetenv(name
, *_NSGetEnviron(), __env_owned
);
505 #endif /* __DARWIN_UNIX03 */