]>
git.saurik.com Git - apple/libc.git/blob - stdlib/setenv-fbsd.c
fdc6fe196b02d4336fc69397a045f0c1e6303ecb
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/lib/libc/stdlib/setenv.c,v 1.9 2002/03/22 21:53:10 obrien Exp $");
43 #include <crt_externs.h>
45 #include <sys/types.h>
47 #include <malloc/malloc.h>
49 #define ZONE_OWNS_PTR(zone, ptr) (malloc_zone_from_ptr((ptr)) == zone)
51 extern malloc_zone_t
*__zone0
;
52 extern void __malloc_check_env_name(const char *);
54 __private_extern__
char *__findenv(const char *, int *, char **);
55 __private_extern__
int __setenv(const char *, const char *, int, int, char ***, malloc_zone_t
*);
56 __private_extern__
void __unsetenv(const char *, char **, malloc_zone_t
*);
58 #ifndef BUILDING_VARIANT
60 * The copy flag may have 3 values:
61 * 1 - make a copy of the name/value pair
62 * 0 - take the name as a user-supplied name=value string
63 * -1 - like 0, except we copy of the name=value string in name
65 __private_extern__
int
66 __setenv(name
, value
, rewrite
, copy
, environp
, envz
)
76 if ((c
= __findenv(name
, &offset
, *environp
))) { /* find if already exists */
81 * In UNIX03, we can overwrite only if we allocated the
82 * string. Then we can realloc it if it is too small.
84 e
= (*environp
)[offset
];
85 if (copy
> 0 && ZONE_OWNS_PTR(envz
, e
)) {
86 size_t l_value
= strlen(value
);
87 if (strlen(c
) < l_value
) { /* old smaller; resize*/
90 if ((r
= realloc(e
, l_value
+ len
+ 1)) == NULL
)
93 (*environp
)[offset
] = r
;
97 while ( (*c
++ = *value
++) );
100 } else { /* create new slot */
104 for (p
= *environp
, cnt
= 0; *p
; ++p
, ++cnt
);
105 if (ZONE_OWNS_PTR(envz
, *environp
)) { /* just increase size */
106 p
= (char **)realloc((char *)*environp
,
107 (size_t)(sizeof(char *) * (cnt
+ 2)));
112 else { /* get new space */
113 /* copy old entries into it */
114 p
= malloc_zone_malloc(envz
, (size_t)(sizeof(char *) * (cnt
+ 2)));
117 bcopy(*environp
, p
, cnt
* sizeof(char *));
120 (*environp
)[cnt
+ 1] = NULL
;
123 /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
124 * strings. For Unix03 putenv(), we put the string directly in
125 * the environment. */
127 for (c
= (char *)name
; *c
&& *c
!= '='; ++c
); /* no `=' in name */
128 if (!((*environp
)[offset
] = /* name + `=' + value */
129 malloc_zone_malloc(envz
, (size_t)((int)(c
- name
) + strlen(value
) + 2))))
131 for (c
= (*environp
)[offset
]; (*c
= *name
++) && *c
!= '='; ++c
);
132 for (*c
++ = '='; (*c
++ = *value
++); );
134 /* the legacy behavior copies the string */
136 size_t len
= strlen(name
);
137 if((c
= malloc_zone_malloc(envz
, len
+ 1)) == NULL
)
139 memcpy(c
, name
, len
+ 1);
142 /* if we malloc-ed the previous value, free it first */
143 if ((*environp
)[offset
] != NULL
&& ZONE_OWNS_PTR(envz
, (*environp
)[offset
]))
144 free((*environp
)[offset
]);
145 (*environp
)[offset
] = (char *)name
;
150 __private_extern__
void
151 __unsetenv(const char *name
, char **environ
, malloc_zone_t
*envz
)
156 while (__findenv(name
, &offset
, environ
)) { /* if set multiple times */
157 /* if we malloc-ed it, free it first */
158 if (ZONE_OWNS_PTR(envz
, environ
[offset
]))
159 free(environ
[offset
]);
160 for (p
= &environ
[offset
];; ++p
)
161 if (!(*p
= *(p
+ 1)))
166 /****************************************************************************/
168 * _allocenvstate -- SPI that creates a new state (opaque)
173 return (void *)malloc_create_zone(1000 /* unused */, 0 /* unused */);
177 * _copyenv -- SPI that copies a NULL-tereminated char * array in a newly
178 * allocated buffer, compatible with the other SPI env routines. If env
179 * is NULL, a char * array composed of a single NULL is returned. NULL
180 * is returned on error. (This isn't needed anymore, as __setenv will
181 * automatically make a copy in the zone.)
190 for (p
= env
; *p
; ++p
, ++cnt
);
191 p
= (char **)malloc((size_t)(sizeof(char *) * cnt
));
195 bcopy(env
, p
, cnt
* sizeof(char *));
202 * _deallocenvstate -- SPI that frees all the memory associated with the state
203 * and all allocated strings, including the environment array itself if it
207 _deallocenvstate(void *state
)
211 if (!(envz
= (malloc_zone_t
*)state
) || envz
== __zone0
) {
215 malloc_destroy_zone(envz
);
220 * setenvp -- SPI using an arbitrary pointer to string array and an env state,
221 * created by _allocenvstate(). Initial checking is not done.
223 * Set the value of the environmental variable "name" to be
224 * "value". If rewrite is set, replace any current value.
227 _setenvp(const char *name
, const char *value
, int rewrite
, char ***envp
, void *state
)
229 /* insure __zone0 is set up */
231 __zone0
= malloc_create_zone(0, 0);
237 return (__setenv(name
, value
, rewrite
, 1, envp
, (state
? (malloc_zone_t
*)state
: __zone0
)));
241 * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
242 * state, created by _allocenvstate(). Initial checking is not done.
244 * Delete environmental variable "name".
247 _unsetenvp(const char *name
, char ***envp
, void *state
)
249 /* insure __zone0 is set up */
251 __zone0
= malloc_create_zone(0, 0);
257 __unsetenv(name
, *envp
, (state
? (malloc_zone_t
*)state
: __zone0
));
261 #endif /* !BUILD_VARIANT */
265 * Set the value of the environmental variable "name" to be
266 * "value". If rewrite is set, replace any current value.
269 setenv(name
, value
, rewrite
)
274 /* no null ptr or empty str */
275 if(name
== NULL
|| *name
== 0) {
282 if (strchr(name
, '=')) {
286 #endif /* __DARWIN_UNIX03 */
288 if (*value
== '=') /* no `=' in value */
290 /* insure __zone0 is set up before calling __malloc_check_env_name */
292 __zone0
= malloc_create_zone(0, 0);
298 __malloc_check_env_name(name
); /* see if we are changing a malloc environment variable */
299 return (__setenv(name
, value
, rewrite
, 1, _NSGetEnviron(), __zone0
));
304 * Delete environmental variable "name".
308 #else /* !__DARWIN_UNIX03 */
310 #endif /* __DARWIN_UNIX03 */
315 /* no null ptr or empty str */
316 if(name
== NULL
|| *name
== 0) {
322 if (strchr(name
, '=')) {
326 /* insure __zone0 is set up before calling __malloc_check_env_name */
328 __zone0
= malloc_create_zone(0, 0);
334 #else /* !__DARWIN_UNIX03 */
335 /* no null ptr or empty str */
336 if(name
== NULL
|| *name
== 0)
338 /* insure __zone0 is set up before calling __malloc_check_env_name */
340 __zone0
= malloc_create_zone(0, 0);
344 #endif /* __DARWIN_UNIX03 */
345 __malloc_check_env_name(name
); /* see if we are changing a malloc environment variable */
346 __unsetenv(name
, *_NSGetEnviron(), __zone0
);
349 #endif /* __DARWIN_UNIX03 */