]> git.saurik.com Git - apple/libc.git/blame - stdlib/setenv-fbsd.c
Libc-498.tar.gz
[apple/libc.git] / stdlib / setenv-fbsd.c
CommitLineData
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.
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34#if defined(LIBC_SCCS) && !defined(lint)
35static 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 $");
39
40#include <stddef.h>
41#include <stdlib.h>
42#include <string.h>
43#include <crt_externs.h>
44#include <errno.h>
45#include <sys/types.h>
46#include <fcntl.h>
47#include <malloc/malloc.h>
48
49#define ZONE_OWNS_PTR(zone, ptr) (malloc_zone_from_ptr((ptr)) == zone)
50
51extern malloc_zone_t *__zone0;
52extern void __malloc_check_env_name(const char *);
53
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 *);
57
58#ifndef BUILDING_VARIANT
59/*
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
64 */
65__private_extern__ int
66__setenv(name, value, rewrite, copy, environp, envz)
67 const char *name;
68 const char *value;
69 int rewrite, copy;
70 char ***environp;
71 malloc_zone_t *envz;
72{
73 char *c;
74 int offset;
75
76 if ((c = __findenv(name, &offset, *environp))) { /* find if already exists */
77 char *e;
78 if (!rewrite)
79 return (0);
80 /*
81 * In UNIX03, we can overwrite only if we allocated the
82 * string. Then we can realloc it if it is too small.
83 */
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*/
88 char *r;
89 size_t len = c - e;
90 if ((r = realloc(e, l_value + len + 1)) == NULL)
91 return (-1);
92 if (r != e) {
93 (*environp)[offset] = r;
94 c = r + len;
95 }
96 }
97 while ( (*c++ = *value++) );
98 return (0);
99 }
100 } else { /* create new slot */
101 int cnt;
102 char **p;
103
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)));
108 if (!p)
109 return (-1);
110 *environp = p;
111 }
112 else { /* get new space */
113 /* copy old entries into it */
114 p = malloc_zone_malloc(envz, (size_t)(sizeof(char *) * (cnt + 2)));
115 if (!p)
116 return (-1);
117 bcopy(*environp, p, cnt * sizeof(char *));
118 *environp = p;
119 }
120 (*environp)[cnt + 1] = NULL;
121 offset = cnt;
122 }
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. */
126 if (copy > 0) {
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))))
130 return (-1);
131 for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
132 for (*c++ = '='; (*c++ = *value++); );
133 } else {
134 /* the legacy behavior copies the string */
135 if (copy < 0) {
136 size_t len = strlen(name);
137 if((c = malloc_zone_malloc(envz, len + 1)) == NULL)
138 return (-1);
139 memcpy(c, name, len + 1);
140 name = c;
141 }
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;
146 }
147 return (0);
148}
149
150__private_extern__ void
151__unsetenv(const char *name, char **environ, malloc_zone_t *envz)
152{
153 char **p;
154 int offset;
155
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)))
162 break;
163 }
164}
165
166/****************************************************************************/
167/*
168 * _allocenvstate -- SPI that creates a new state (opaque)
169 */
170void *
171_allocenvstate(void)
172{
173 return (void *)malloc_create_zone(1000 /* unused */, 0 /* unused */);
174}
175
176/*
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.)
182 */
183char **
184_copyenv(char **env)
185{
186 char **p;
187 int cnt = 1;
188
189 if (env)
190 for (p = env; *p; ++p, ++cnt);
191 p = (char **)malloc((size_t)(sizeof(char *) * cnt));
192 if (!p)
193 return (NULL);
194 if (env)
195 bcopy(env, p, cnt * sizeof(char *));
196 else
197 *p = NULL;
198 return p;
199}
200
201/*
202 * _deallocenvstate -- SPI that frees all the memory associated with the state
203 * and all allocated strings, including the environment array itself if it
204 * was copied.
205 */
206int
207_deallocenvstate(void *state)
208{
209 malloc_zone_t *envz;
210
211 if (!(envz = (malloc_zone_t *)state) || envz == __zone0) {
212 errno = EINVAL;
213 return -1;
214 }
215 malloc_destroy_zone(envz);
216 return 0;
217}
218
219/*
220 * setenvp -- SPI using an arbitrary pointer to string array and an env state,
221 * created by _allocenvstate(). Initial checking is not done.
222 *
223 * Set the value of the environmental variable "name" to be
224 * "value". If rewrite is set, replace any current value.
225 */
226int
227_setenvp(const char *name, const char *value, int rewrite, char ***envp, void *state)
228{
229 /* insure __zone0 is set up */
230 if (!__zone0) {
231 __zone0 = malloc_create_zone(0, 0);
232 if (!__zone0) {
233 errno = ENOMEM;
234 return (-1);
235 }
236 }
237 return (__setenv(name, value, rewrite, 1, envp, (state ? (malloc_zone_t *)state : __zone0)));
238}
239
240/*
241 * unsetenv(name) -- SPI using an arbitrary pointer to string array and an env
242 * state, created by _allocenvstate(). Initial checking is not done.
243 *
244 * Delete environmental variable "name".
245 */
246int
247_unsetenvp(const char *name, char ***envp, void *state)
248{
249 /* insure __zone0 is set up */
250 if (!__zone0) {
251 __zone0 = malloc_create_zone(0, 0);
252 if (!__zone0) {
253 errno = ENOMEM;
254 return (-1);
255 }
256 }
257 __unsetenv(name, *envp, (state ? (malloc_zone_t *)state : __zone0));
258 return 0;
259}
260
261#endif /* !BUILD_VARIANT */
262
263/*
264 * setenv --
265 * Set the value of the environmental variable "name" to be
266 * "value". If rewrite is set, replace any current value.
267 */
268int
269setenv(name, value, rewrite)
270 const char *name;
271 const char *value;
272 int rewrite;
273{
274 /* no null ptr or empty str */
275 if(name == NULL || *name == 0) {
276 errno = EINVAL;
277 return (-1);
278 }
279
280#if __DARWIN_UNIX03
281 /* no '=' in name */
282 if (strchr(name, '=')) {
283 errno = EINVAL;
284 return (-1);
285 }
286#endif /* __DARWIN_UNIX03 */
287
288 if (*value == '=') /* no `=' in value */
289 ++value;
290 /* insure __zone0 is set up before calling __malloc_check_env_name */
291 if (!__zone0) {
292 __zone0 = malloc_create_zone(0, 0);
293 if (!__zone0) {
294 errno = ENOMEM;
295 return (-1);
296 }
297 }
298 __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
299 return (__setenv(name, value, rewrite, 1, _NSGetEnviron(), __zone0));
300}
301
302/*
303 * unsetenv(name) --
304 * Delete environmental variable "name".
305 */
306#if __DARWIN_UNIX03
307int
308#else /* !__DARWIN_UNIX03 */
309void
310#endif /* __DARWIN_UNIX03 */
311unsetenv(name)
312 const char *name;
313{
314#if __DARWIN_UNIX03
315 /* no null ptr or empty str */
316 if(name == NULL || *name == 0) {
317 errno = EINVAL;
318 return (-1);
319 }
320
321 /* no '=' in name */
322 if (strchr(name, '=')) {
323 errno = EINVAL;
324 return (-1);
325 }
326 /* insure __zone0 is set up before calling __malloc_check_env_name */
327 if (!__zone0) {
328 __zone0 = malloc_create_zone(0, 0);
329 if (!__zone0) {
330 errno = ENOMEM;
331 return (-1);
332 }
333 }
334#else /* !__DARWIN_UNIX03 */
335 /* no null ptr or empty str */
336 if(name == NULL || *name == 0)
337 return;
338 /* insure __zone0 is set up before calling __malloc_check_env_name */
339 if (!__zone0) {
340 __zone0 = malloc_create_zone(0, 0);
341 if (!__zone0)
342 return;
343 }
344#endif /* __DARWIN_UNIX03 */
345 __malloc_check_env_name(name); /* see if we are changing a malloc environment variable */
346 __unsetenv(name, *_NSGetEnviron(), __zone0);
347#if __DARWIN_UNIX03
348 return 0;
349#endif /* __DARWIN_UNIX03 */
350}