Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | --- setenv.c.orig 2003-05-20 15:23:25.000000000 -0700 +++ setenv.c 2004-11-05 17:15:11.000000000 -0800 @@ -40,81 +40,137 @@ #include <stddef.h> #include <stdlib.h> #include <string.h> +#include <crt_externs.h> +#include <errno.h> char *__findenv(const char *, int *); +__private_extern__ int __setenv(const char *, const char *, int, int); -/* - * setenv -- - * Set the value of the environmental variable "name" to be - * "value". If rewrite is set, replace any current value. - */ -int -setenv(name, value, rewrite) +#ifndef BUILDING_VARIANT +__private_extern__ int +__setenv(name, value, rewrite, copy) const char *name; const char *value; - int rewrite; + int rewrite, copy; { - extern char **environ; + char ***environp = _NSGetEnviron(); static char **alloced; /* if allocated space before */ char *c; - int l_value, offset; + int offset; - if (*value == '=') /* no `=' in value */ - ++value; - l_value = strlen(value); if ((c = __findenv(name, &offset))) { /* find if already exists */ if (!rewrite) return (0); - if (strlen(c) >= l_value) { /* old larger; copy over */ - while ( (*c++ = *value++) ); - return (0); - } + /* In UNIX03, we can't overwrite even if the string is long + * enough, because the putenv() string is owned by the user + * (ie, always malloc() a new string) */ } else { /* create new slot */ int cnt; char **p; - for (p = environ, cnt = 0; *p; ++p, ++cnt); - if (alloced == environ) { /* just increase size */ - p = (char **)realloc((char *)environ, + for (p = *environp, cnt = 0; *p; ++p, ++cnt); + if (alloced == *environp) { /* just increase size */ + p = (char **)realloc((char *)*environp, (size_t)(sizeof(char *) * (cnt + 2))); if (!p) return (-1); - alloced = environ = p; + alloced = *environp = p; } else { /* get new space */ /* copy old entries into it */ p = malloc((size_t)(sizeof(char *) * (cnt + 2))); if (!p) return (-1); - bcopy(environ, p, cnt * sizeof(char *)); - alloced = environ = p; + bcopy(*environp, p, cnt * sizeof(char *)); + alloced = *environp = p; } - environ[cnt + 1] = NULL; + (*environp)[cnt + 1] = NULL; offset = cnt; } - for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */ - if (!(environ[offset] = /* name + `=' + value */ - malloc((size_t)((int)(c - name) + l_value + 2)))) - return (-1); - for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); - for (*c++ = '='; (*c++ = *value++); ); + /* For non Unix03, or UnixO3 setenv(), we make a copy of the user's + * strings. For Unix03 putenv(), we put the string directly in + * the environment. */ + if (copy) { + for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */ + if (!((*environp)[offset] = /* name + `=' + value */ + malloc((size_t)((int)(c - name) + strlen(value) + 2)))) + return (-1); + for (c = (*environp)[offset]; (*c = *name++) && *c != '='; ++c); + for (*c++ = '='; (*c++ = *value++); ); + } else + (*environp)[offset] = name; return (0); } +#endif /* !BUILD_VARIANT */ + +/* + * setenv -- + * Set the value of the environmental variable "name" to be + * "value". If rewrite is set, replace any current value. + */ +int +setenv(name, value, rewrite) + const char *name; + const char *value; + int rewrite; +{ + /* no null ptr or empty str */ + if(name == NULL || *name == 0) { + errno = EINVAL; + return (-1); + } + +#if __DARWIN_UNIX03 + /* no '=' in name */ + if (strchr(name, '=')) { + errno = EINVAL; + return (-1); + } +#endif /* __DARWIN_UNIX03 */ + + if (*value == '=') /* no `=' in value */ + ++value; + return (__setenv(name, value, rewrite, 1)); +} /* * unsetenv(name) -- * Delete environmental variable "name". */ +#if __DARWIN_UNIX03 +int +#else /* !__DARWIN_UNIX03 */ void +#endif /* __DARWIN_UNIX03 */ unsetenv(name) const char *name; { - extern char **environ; + char **environ = *_NSGetEnviron(); char **p; int offset; +#if __DARWIN_UNIX03 + /* no null ptr or empty str */ + if(name == NULL || *name == 0) { + errno = EINVAL; + return (-1); + } + + /* no '=' in name */ + if (strchr(name, '=')) { + errno = EINVAL; + return (-1); + } +#else /* !__DARWIN_UNIX03 */ + /* no null ptr or empty str */ + if(name == NULL || *name == 0) + return; +#endif /* __DARWIN_UNIX03 */ while (__findenv(name, &offset)) /* if set multiple times */ for (p = &environ[offset];; ++p) if (!(*p = *(p + 1))) break; +#if __DARWIN_UNIX03 + return 0; +#endif /* __DARWIN_UNIX03 */ } |