string-desc: Fix undefined behaviour.
[gnulib.git] / lib / putenv.c
blobd3084c9e6077d59f48a6dba86499539a14f77608
1 /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2024 Free Software
2 Foundation, Inc.
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 file is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation, either version 3 of the
10 License, or (at your option) any later version.
12 This file 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 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20 #include <config.h>
22 /* Specification. */
23 #include <stdlib.h>
25 #include <stddef.h>
27 /* Include errno.h *after* sys/types.h to work around header problems
28 on AIX 3.2.5. */
29 #include <errno.h>
30 #ifndef __set_errno
31 # define __set_errno(ev) ((errno) = (ev))
32 #endif
34 #include <string.h>
35 #include <unistd.h>
37 #if defined _WIN32 && ! defined __CYGWIN__
38 # define WIN32_LEAN_AND_MEAN
39 # include <windows.h>
40 #endif
42 #if _LIBC
43 # if HAVE_GNU_LD
44 # define environ __environ
45 # else
46 extern char **environ;
47 # endif
48 #endif
50 #if _LIBC
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)
56 #else
57 # define LOCK
58 # define UNLOCK
59 #endif
61 #if defined _WIN32 && ! defined __CYGWIN__
62 /* Don't assume that UNICODE is not defined. */
63 # undef SetEnvironmentVariable
64 # define SetEnvironmentVariable SetEnvironmentVariableA
65 #endif
67 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
68 If STRING contains no '=', then remove STRING from the environment. */
69 int
70 putenv (char *string)
72 const char *name_end = strchr (string, '=');
73 char **ep;
75 if (name_end == NULL)
77 /* Remove the variable from the environment. */
78 return unsetenv (string);
81 #if HAVE_DECL__PUTENV /* native Windows */
82 /* The Microsoft documentation
83 <https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-wputenv>
84 says:
85 "Don't change an environment entry directly: instead,
86 use _putenv or _wputenv to change it."
87 Note: Microsoft's _putenv updates not only the contents of _environ but
88 also the contents of _wenviron, so that both are in kept in sync.
90 If we didn't follow this advice, our code and other parts of the
91 application (that use _putenv) would fight over who owns the environ vector
92 and thus cause a crash. */
93 if (name_end[1])
94 return _putenv (string);
95 else
97 /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
98 to allocate the environ vector and then replace the new
99 entry with "NAME=". */
100 int putenv_result;
101 char *name_x = malloc (name_end - string + sizeof "= ");
102 if (!name_x)
103 return -1;
104 memcpy (name_x, string, name_end - string + 1);
105 name_x[name_end - string + 1] = ' ';
106 name_x[name_end - string + 2] = 0;
107 putenv_result = _putenv (name_x);
108 for (ep = environ; *ep; ep++)
109 if (strcmp (*ep, name_x) == 0)
111 *ep = string;
112 break;
114 # if defined _WIN32 && ! defined __CYGWIN__
115 if (putenv_result == 0)
117 /* _putenv propagated "NAME= " into the subprocess environment;
118 fix that by calling SetEnvironmentVariable directly. */
119 name_x[name_end - string] = 0;
120 putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
121 errno = ENOMEM; /* ENOMEM is the only way to fail. */
123 # endif
124 free (name_x);
125 return putenv_result;
127 #else
128 for (ep = environ; *ep; ep++)
129 if (strncmp (*ep, string, name_end - string) == 0
130 && (*ep)[name_end - string] == '=')
131 break;
133 if (*ep)
134 *ep = string;
135 else
137 static char **last_environ = NULL;
138 size_t size = ep - environ;
139 char **new_environ = malloc ((size + 2) * sizeof *new_environ);
140 if (! new_environ)
141 return -1;
142 new_environ[0] = string;
143 memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
144 free (last_environ);
145 last_environ = new_environ;
146 environ = new_environ;
149 return 0;
150 #endif