1 --- setenv.c.orig 2003-05-20 15:23:25.000000000 -0700
2 +++ setenv.c 2004-11-05 17:15:11.000000000 -0800
7 +#include <crt_externs.h>
10 char *__findenv(const char *, int *);
11 +__private_extern__ int __setenv(const char *, const char *, int, int);
15 - * Set the value of the environmental variable "name" to be
16 - * "value". If rewrite is set, replace any current value.
19 -setenv(name, value, rewrite)
20 +#ifndef BUILDING_VARIANT
21 +__private_extern__ int
22 +__setenv(name, value, rewrite, copy)
28 - extern char **environ;
29 + char ***environp = _NSGetEnviron();
30 static char **alloced; /* if allocated space before */
32 - int l_value, offset;
35 - if (*value == '=') /* no `=' in value */
37 - l_value = strlen(value);
38 if ((c = __findenv(name, &offset))) { /* find if already exists */
41 - if (strlen(c) >= l_value) { /* old larger; copy over */
42 - while ( (*c++ = *value++) );
45 + /* In UNIX03, we can't overwrite even if the string is long
46 + * enough, because the putenv() string is owned by the user
47 + * (ie, always malloc() a new string) */
48 } else { /* create new slot */
52 - for (p = environ, cnt = 0; *p; ++p, ++cnt);
53 - if (alloced == environ) { /* just increase size */
54 - p = (char **)realloc((char *)environ,
55 + for (p = *environp, cnt = 0; *p; ++p, ++cnt);
56 + if (alloced == *environp) { /* just increase size */
57 + p = (char **)realloc((char *)*environp,
58 (size_t)(sizeof(char *) * (cnt + 2)));
61 - alloced = environ = p;
62 + alloced = *environp = p;
64 else { /* get new space */
65 /* copy old entries into it */
66 p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
69 - bcopy(environ, p, cnt * sizeof(char *));
70 - alloced = environ = p;
71 + bcopy(*environp, p, cnt * sizeof(char *));
72 + alloced = *environp = p;
74 - environ[cnt + 1] = NULL;
75 + (*environp)[cnt + 1] = NULL;
78 - for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
79 - if (!(environ[offset] = /* name + `=' + value */
80 - malloc((size_t)((int)(c - name) + l_value + 2))))
82 - for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
83 - for (*c++ = '='; (*c++ = *value++); );
84 + /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's
85 + * strings. For Unix03 putenv(), we put the string directly in
86 + * the environment. */
88 + for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
89 + if (!((*environp)[offset] = /* name + `=' + value */
90 + malloc((size_t)((int)(c - name) + strlen(value) + 2))))
92 + for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
93 + for (*c++ = '='; (*c++ = *value++); );
95 + (*environp)[offset] = name;
98 +#endif /* !BUILD_VARIANT */
102 + * Set the value of the environmental variable "name" to be
103 + * "value". If rewrite is set, replace any current value.
106 +setenv(name, value, rewrite)
111 + /* no null ptr or empty str */
112 + if(name == NULL || *name == 0) {
118 + /* no '=' in name */
119 + if (strchr(name, '=')) {
123 +#endif /* __DARWIN_UNIX03 */
125 + if (*value == '=') /* no `=' in value */
127 + return (__setenv(name, value, rewrite, 1));
132 * Delete environmental variable "name".
136 +#else /* !__DARWIN_UNIX03 */
138 +#endif /* __DARWIN_UNIX03 */
142 - extern char **environ;
143 + char **environ = *_NSGetEnviron();
148 + /* no null ptr or empty str */
149 + if(name == NULL || *name == 0) {
154 + /* no '=' in name */
155 + if (strchr(name, '=')) {
159 +#else /* !__DARWIN_UNIX03 */
160 + /* no null ptr or empty str */
161 + if(name == NULL || *name == 0)
163 +#endif /* __DARWIN_UNIX03 */
164 while (__findenv(name, &offset)) /* if set multiple times */
165 for (p = &environ[offset];; ++p)
166 if (!(*p = *(p + 1)))
170 +#endif /* __DARWIN_UNIX03 */