1 /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2021 Free Software
4 NOTE: The canonical source of this file is maintained with the GNU C
5 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
7 This program is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
27 /* Include errno.h *after* sys/types.h to work around header problems
31 # define __set_errno(ev) ((errno) = (ev))
37 #if defined _WIN32 && ! defined __CYGWIN__
38 # define WIN32_LEAN_AND_MEAN
44 # define environ __environ
46 extern char **environ;
51 /* This lock protects against simultaneous modifications of 'environ'. */
52 # include <bits/libc-lock.h>
53 __libc_lock_define_initialized (static, envlock)
54 # define LOCK __libc_lock_lock (envlock)
55 # define UNLOCK __libc_lock_unlock (envlock)
61 #if defined _WIN32 && ! defined __CYGWIN__
62 /* Don't assume that UNICODE is not defined. */
63 # undef SetEnvironmentVariable
64 # define SetEnvironmentVariable SetEnvironmentVariableA
68 _unsetenv (const char *name)
71 #if !HAVE_DECL__PUTENV
75 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
86 char *name_ = malloc (len + 2);
87 memcpy (name_, name, len);
90 putenv_result = _putenv (name_);
100 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
102 /* Found it. Remove this pointer by moving later ones back. */
108 /* Continue the loop in case NAME appears again. */
120 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
121 If STRING contains no '=', then remove STRING from the environment. */
123 putenv (char *string)
125 const char *name_end = strchr (string, '=');
128 if (name_end == NULL)
130 /* Remove the variable from the environment. */
131 return _unsetenv (string);
134 #if HAVE_DECL__PUTENV
135 /* Rely on _putenv to allocate the new environment. If other
136 parts of the application use _putenv, the !HAVE_DECL__PUTENV code
137 would fight over who owns the environ vector, causing a crash. */
139 return _putenv (string);
142 /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
143 to allocate the environ vector and then replace the new
144 entry with "NAME=". */
146 char *name_x = malloc (name_end - string + sizeof "= ");
149 memcpy (name_x, string, name_end - string + 1);
150 name_x[name_end - string + 1] = ' ';
151 name_x[name_end - string + 2] = 0;
152 putenv_result = _putenv (name_x);
153 for (ep = environ; *ep; ep++)
154 if (strcmp (*ep, name_x) == 0)
159 # if defined _WIN32 && ! defined __CYGWIN__
160 if (putenv_result == 0)
162 /* _putenv propagated "NAME= " into the subprocess environment;
163 fix that by calling SetEnvironmentVariable directly. */
164 name_x[name_end - string] = 0;
165 putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
166 errno = ENOMEM; /* ENOMEM is the only way to fail. */
170 return putenv_result;
173 for (ep = environ; *ep; ep++)
174 if (strncmp (*ep, string, name_end - string) == 0
175 && (*ep)[name_end - string] == '=')
182 static char **last_environ = NULL;
183 size_t size = ep - environ;
184 char **new_environ = malloc ((size + 2) * sizeof *new_environ);
187 new_environ[0] = string;
188 memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
190 last_environ = new_environ;
191 environ = new_environ;