Debian Codegraph @

src/setenv.c

raw
1 /*
2 libfakechroot -- fake chroot environment
3 Copyright (c) 2010, 2013 Piotr Roszatycki <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <config.h>
21
22 #include "libfakechroot.h"
23 #include "strchrnul.h"
24
25
26 /* Copyright (C) 1992,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
27 This file is part of the GNU C Library.
28
29 The GNU C Library is free software; you can redistribute it and/or
30 modify it under the terms of the GNU Lesser General Public
31 License as published by the Free Software Foundation; either
32 version 2.1 of the License, or (at your option) any later version.
33
34 The GNU C Library is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 Lesser General Public License for more details.
38
39 You should have received a copy of the GNU Lesser General Public
40 License along with the GNU C Library; see the file COPYING.LIB. If
41 not, see <http://www.gnu.org/licenses/>.
42
43 modified for uClibc by Erik Andersen <[email protected]>
44 */
45
46 #include <features.h>
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52
53 /* If this variable is not a null pointer we allocated the current
54 environment. */
55 static char **last_environ;
56
57
58 /* This function is used by `setenv' and `putenv'. The difference between
59 the two functions is that for the former must create a new string which
60 is then placed in the environment, while the argument of `putenv'
61 must be used directly. This is all complicated by the fact that we try
62 to reuse values once generated for a `setenv' call since we can never
63 free the strings. [in uclibc, we do not] */
64 static int __add_to_environ(const char *name, const char *value,
65 int replace)
66 {
67 register char **ep;
68 register size_t size;
69 char *var_val;
70 char **new_environ;
71 /* name may come from putenv() and thus may contain "=VAL" part */
72 const size_t namelen = strchrnul(name, '=') - name;
73 int rv = -1;
74
75 /* We have to get the pointer now that we have the lock and not earlier
76 since another thread might have created a new environment. */
77 ep = __environ;
78
79 size = 0;
80 if (ep != NULL) {
81 while (*ep != NULL) {
82 if (!strncmp(*ep, name, namelen) && (*ep)[namelen] == '=') {
83 /* Found */
84 if (!replace)
85 goto DONE_OK;
86 goto REPLACE;
87 }
88 ++size;
89 ++ep;
90 }
91 }
92
93 /* Not found, add at the end */
94
95 /* We allocated this space; we can extend it. */
96 new_environ = realloc(last_environ, (size + 2) * sizeof(char *));
97 if (new_environ == NULL) {
98 __set_errno(ENOMEM);
99 goto DONE;
100 }
101 if (__environ != last_environ) {
102 memcpy(new_environ, __environ, size * sizeof(char *));
103 }
104 last_environ = __environ = new_environ;
105
106 ep = &new_environ[size];
107 /* Ensure env is NULL terminated in case malloc below fails */
108 ep[0] = NULL;
109 ep[1] = NULL;
110
111 REPLACE:
112 var_val = (char*) name;
113 /* Build VAR=VAL if we called by setenv, not putenv. */
114 if (value != NULL) {
115 const size_t vallen = strlen(value) + 1;
116
117 var_val = malloc(namelen + 1 + vallen);
118 if (var_val == NULL) {
119 __set_errno(ENOMEM);
120 goto DONE;
121 }
122 memcpy(var_val, name, namelen);
123 var_val[namelen] = '=';
124 memcpy(&var_val[namelen + 1], value, vallen);
125 }
126 *ep = var_val;
127
128 DONE_OK:
129 rv = 0;
130
131 DONE:
132 return rv;
133 }
134
135 LOCAL int __setenv(const char *name, const char *value, int replace)
136 {
137 /* NB: setenv("VAR", NULL, 1) inserts "VAR=" string */
138 return __add_to_environ(name, value ? value : "", replace);
139 }
140
141 LOCAL int __unsetenv(const char *name)
142 {
143 const char *eq;
144 size_t len;
145 char **ep;
146
147 if (name == NULL || *name == '\0'
148 || *(eq = strchrnul(name, '=')) == '='
149 ) {
150 __set_errno(EINVAL);
151 return -1;
152 }
153 len = eq - name; /* avoiding strlen this way */
154
155 ep = __environ;
156 /* NB: clearenv(); unsetenv("foo"); should not segfault */
157 if (ep) while (*ep != NULL) {
158 if (!strncmp(*ep, name, len) && (*ep)[len] == '=') {
159 /* Found it. Remove this pointer by moving later ones back. */
160 char **dp = ep;
161 do {
162 dp[0] = dp[1];
163 } while (*dp++);
164 /* Continue the loop in case NAME appears again. */
165 } else {
166 ++ep;
167 }
168 }
169 return 0;
170 }
171
172 /* The `clearenv' was planned to be added to POSIX.1 but probably
173 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
174 for Fortran 77) requires this function. */
175 LOCAL int __clearenv(void)
176 {
177 /* If we allocated this environment we can free it.
178 * If we did not allocate this environment, it's NULL already
179 * and is safe to free(). */
180 free(last_environ);
181 last_environ = NULL;
182 /* Clearing environ removes the whole environment. */
183 __environ = NULL;
184 return 0;
185 }
186
187 /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
188 LOCAL int __putenv(char *string)
189 {
190 if (strchr(string, '=') != NULL) {
191 return __add_to_environ(string, NULL, 1);
192 }
193 return __unsetenv(string);
194 }