]> git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/setenv.c.patch
Libc-391.1.21.tar.gz
[apple/libc.git] / stdlib / FreeBSD / setenv.c.patch
1 --- setenv.c.orig 2003-05-20 15:23:25.000000000 -0700
2 +++ setenv.c 2004-11-05 17:15:11.000000000 -0800
3 @@ -40,81 +40,137 @@
4 #include <stddef.h>
5 #include <stdlib.h>
6 #include <string.h>
7 +#include <crt_externs.h>
8 +#include <errno.h>
9
10 char *__findenv(const char *, int *);
11 +__private_extern__ int __setenv(const char *, const char *, int, int);
12
13 -/*
14 - * setenv --
15 - * Set the value of the environmental variable "name" to be
16 - * "value". If rewrite is set, replace any current value.
17 - */
18 -int
19 -setenv(name, value, rewrite)
20 +#ifndef BUILDING_VARIANT
21 +__private_extern__ int
22 +__setenv(name, value, rewrite, copy)
23 const char *name;
24 const char *value;
25 - int rewrite;
26 + int rewrite, copy;
27 {
28 - extern char **environ;
29 + char ***environp = _NSGetEnviron();
30 static char **alloced; /* if allocated space before */
31 char *c;
32 - int l_value, offset;
33 + int offset;
34
35 - if (*value == '=') /* no `=' in value */
36 - ++value;
37 - l_value = strlen(value);
38 if ((c = __findenv(name, &offset))) { /* find if already exists */
39 if (!rewrite)
40 return (0);
41 - if (strlen(c) >= l_value) { /* old larger; copy over */
42 - while ( (*c++ = *value++) );
43 - return (0);
44 - }
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 */
49 int cnt;
50 char **p;
51
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)));
59 if (!p)
60 return (-1);
61 - alloced = environ = p;
62 + alloced = *environp = p;
63 }
64 else { /* get new space */
65 /* copy old entries into it */
66 p = malloc((size_t)(sizeof(char *) * (cnt + 2)));
67 if (!p)
68 return (-1);
69 - bcopy(environ, p, cnt * sizeof(char *));
70 - alloced = environ = p;
71 + bcopy(*environp, p, cnt * sizeof(char *));
72 + alloced = *environp = p;
73 }
74 - environ[cnt + 1] = NULL;
75 + (*environp)[cnt + 1] = NULL;
76 offset = cnt;
77 }
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))))
81 - return (-1);
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. */
87 + if (copy) {
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))))
91 + return (-1);
92 + for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c);
93 + for (*c++ = '='; (*c++ = *value++); );
94 + } else
95 + (*environp)[offset] = name;
96 return (0);
97 }
98 +#endif /* !BUILD_VARIANT */
99 +
100 +/*
101 + * setenv --
102 + * Set the value of the environmental variable "name" to be
103 + * "value". If rewrite is set, replace any current value.
104 + */
105 +int
106 +setenv(name, value, rewrite)
107 + const char *name;
108 + const char *value;
109 + int rewrite;
110 +{
111 + /* no null ptr or empty str */
112 + if(name == NULL || *name == 0) {
113 + errno = EINVAL;
114 + return (-1);
115 + }
116 +
117 +#if __DARWIN_UNIX03
118 + /* no '=' in name */
119 + if (strchr(name, '=')) {
120 + errno = EINVAL;
121 + return (-1);
122 + }
123 +#endif /* __DARWIN_UNIX03 */
124 +
125 + if (*value == '=') /* no `=' in value */
126 + ++value;
127 + return (__setenv(name, value, rewrite, 1));
128 +}
129
130 /*
131 * unsetenv(name) --
132 * Delete environmental variable "name".
133 */
134 +#if __DARWIN_UNIX03
135 +int
136 +#else /* !__DARWIN_UNIX03 */
137 void
138 +#endif /* __DARWIN_UNIX03 */
139 unsetenv(name)
140 const char *name;
141 {
142 - extern char **environ;
143 + char **environ = *_NSGetEnviron();
144 char **p;
145 int offset;
146
147 +#if __DARWIN_UNIX03
148 + /* no null ptr or empty str */
149 + if(name == NULL || *name == 0) {
150 + errno = EINVAL;
151 + return (-1);
152 + }
153 +
154 + /* no '=' in name */
155 + if (strchr(name, '=')) {
156 + errno = EINVAL;
157 + return (-1);
158 + }
159 +#else /* !__DARWIN_UNIX03 */
160 + /* no null ptr or empty str */
161 + if(name == NULL || *name == 0)
162 + return;
163 +#endif /* __DARWIN_UNIX03 */
164 while (__findenv(name, &offset)) /* if set multiple times */
165 for (p = &environ[offset];; ++p)
166 if (!(*p = *(p + 1)))
167 break;
168 +#if __DARWIN_UNIX03
169 + return 0;
170 +#endif /* __DARWIN_UNIX03 */
171 }