| 1 |
/* Copyright (C) 1992, 1995-2003, 2005-2026 Free Software Foundation, Inc. |
| 2 |
This file is part of the GNU C Library. |
| 3 |
|
| 4 |
This file is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License as |
| 6 |
published by the Free Software Foundation; either version 2.1 of the |
| 7 |
License, or (at your option) any later version. |
| 8 |
|
| 9 |
This file is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU Lesser General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#if !_LIBC |
| 18 |
/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc |
| 19 |
optimizes away the name == NULL test below. */ |
| 20 |
# define _GL_ARG_NONNULL(params) |
| 21 |
|
| 22 |
# define _GL_USE_STDLIB_ALLOC 1 |
| 23 |
# include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <alloca.h> |
| 27 |
|
| 28 |
/* Specification. */ |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
#include <errno.h> |
| 32 |
#ifndef __set_errno |
| 33 |
# define __set_errno(ev) ((errno) = (ev)) |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <string.h> |
| 37 |
#if _LIBC || HAVE_UNISTD_H |
| 38 |
# include <unistd.h> |
| 39 |
#endif |
| 40 |
|
| 41 |
#if defined _WIN32 && ! defined __CYGWIN__ |
| 42 |
# define WIN32_LEAN_AND_MEAN |
| 43 |
# include <windows.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#if !_LIBC |
| 47 |
# include "malloca.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
#if defined _WIN32 && ! defined __CYGWIN__ |
| 51 |
/* Don't assume that UNICODE is not defined. */ |
| 52 |
# undef SetEnvironmentVariable |
| 53 |
# define SetEnvironmentVariable SetEnvironmentVariableA |
| 54 |
#endif |
| 55 |
|
| 56 |
#if _LIBC || !HAVE_SETENV |
| 57 |
#if !HAVE_DECL__PUTENV |
| 58 |
|
| 59 |
#if !_LIBC |
| 60 |
# define __environ environ |
| 61 |
#endif |
| 62 |
|
| 63 |
#if _LIBC |
| 64 |
/* This lock protects against simultaneous modifications of 'environ'. */ |
| 65 |
# include <bits/libc-lock.h> |
| 66 |
__libc_lock_define_initialized (static, envlock) |
| 67 |
# define LOCK __libc_lock_lock (envlock) |
| 68 |
# define UNLOCK __libc_lock_unlock (envlock) |
| 69 |
#else |
| 70 |
# define LOCK |
| 71 |
# define UNLOCK |
| 72 |
#endif |
| 73 |
|
| 74 |
/* In the GNU C library we must keep the namespace clean. */ |
| 75 |
#ifdef _LIBC |
| 76 |
# define setenv __setenv |
| 77 |
# define clearenv __clearenv |
| 78 |
# define tfind __tfind |
| 79 |
# define tsearch __tsearch |
| 80 |
#endif |
| 81 |
|
| 82 |
/* In the GNU C library implementation we try to be more clever and |
| 83 |
allow arbitrarily many changes of the environment given that the used |
| 84 |
values are from a small set. Outside glibc this will eat up all |
| 85 |
memory after a while. */ |
| 86 |
#if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \ |
| 87 |
&& (defined __GNUC__ || defined __clang__)) |
| 88 |
# define USE_TSEARCH 1 |
| 89 |
# include <search.h> |
| 90 |
typedef int (*compar_fn_t) (const void *, const void *); |
| 91 |
|
| 92 |
/* This is a pointer to the root of the search tree with the known |
| 93 |
values. */ |
| 94 |
static void *known_values; |
| 95 |
|
| 96 |
# define KNOWN_VALUE(Str) \ |
| 97 |
__extension__ \ |
| 98 |
({ \ |
| 99 |
void *value = tfind (Str, &known_values, (compar_fn_t) strcmp); \ |
| 100 |
value != NULL ? *(char **) value : NULL; \ |
| 101 |
}) |
| 102 |
# define STORE_VALUE(Str) \ |
| 103 |
tsearch (Str, &known_values, (compar_fn_t) strcmp) |
| 104 |
|
| 105 |
#else |
| 106 |
# undef USE_TSEARCH |
| 107 |
|
| 108 |
# define KNOWN_VALUE(Str) NULL |
| 109 |
# define STORE_VALUE(Str) do { } while (0) |
| 110 |
|
| 111 |
#endif |
| 112 |
|
| 113 |
|
| 114 |
/* If this variable is not a null pointer we allocated the current |
| 115 |
environment. */ |
| 116 |
static char **last_environ; |
| 117 |
|
| 118 |
|
| 119 |
/* This function is used by 'setenv' and 'putenv'. The difference between |
| 120 |
the two functions is that for the former must create a new string which |
| 121 |
is then placed in the environment, while the argument of 'putenv' |
| 122 |
must be used directly. This is all complicated by the fact that we try |
| 123 |
to reuse values once generated for a 'setenv' call since we can never |
| 124 |
free the strings. */ |
| 125 |
int |
| 126 |
__add_to_environ (const char *name, const char *value, const char *combined, |
| 127 |
int replace) |
| 128 |
{ |
| 129 |
const size_t namelen = strlen (name); |
| 130 |
const size_t vallen = value != NULL ? strlen (value) + 1 : 0; |
| 131 |
|
| 132 |
LOCK; |
| 133 |
|
| 134 |
/* We have to get the pointer now that we have the lock and not earlier |
| 135 |
since another thread might have created a new environment. */ |
| 136 |
char **ep = __environ; |
| 137 |
|
| 138 |
size_t size = 0; |
| 139 |
if (ep != NULL) |
| 140 |
{ |
| 141 |
for (; *ep != NULL; ++ep) |
| 142 |
if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=') |
| 143 |
break; |
| 144 |
else |
| 145 |
++size; |
| 146 |
} |
| 147 |
|
| 148 |
if (ep == NULL || *ep == NULL) |
| 149 |
{ |
| 150 |
/* We allocated this space; we can extend it. */ |
| 151 |
char **new_environ = |
| 152 |
(char **) (last_environ == NULL |
| 153 |
? malloc ((size + 2) * sizeof (char *)) |
| 154 |
: realloc (last_environ, (size + 2) * sizeof (char *))); |
| 155 |
if (new_environ == NULL) |
| 156 |
{ |
| 157 |
/* It's easier to set errno to ENOMEM than to rely on the |
| 158 |
'malloc-posix' and 'realloc-posix' gnulib modules. */ |
| 159 |
__set_errno (ENOMEM); |
| 160 |
UNLOCK; |
| 161 |
return -1; |
| 162 |
} |
| 163 |
|
| 164 |
/* If the whole entry is given add it. */ |
| 165 |
if (combined != NULL) |
| 166 |
/* We must not add the string to the search tree since it belongs |
| 167 |
to the user. */ |
| 168 |
new_environ[size] = (char *) combined; |
| 169 |
else |
| 170 |
{ |
| 171 |
/* See whether the value is already known. */ |
| 172 |
#ifdef USE_TSEARCH |
| 173 |
char *new_value; |
| 174 |
# ifdef _LIBC |
| 175 |
new_value = (char *) alloca (namelen + 1 + vallen); |
| 176 |
__mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), |
| 177 |
value, vallen); |
| 178 |
# else |
| 179 |
new_value = (char *) malloca (namelen + 1 + vallen); |
| 180 |
if (new_value == NULL) |
| 181 |
{ |
| 182 |
__set_errno (ENOMEM); |
| 183 |
UNLOCK; |
| 184 |
return -1; |
| 185 |
} |
| 186 |
memcpy (new_value, name, namelen); |
| 187 |
new_value[namelen] = '='; |
| 188 |
memcpy (&new_value[namelen + 1], value, vallen); |
| 189 |
# endif |
| 190 |
|
| 191 |
new_environ[size] = KNOWN_VALUE (new_value); |
| 192 |
if (new_environ[size] == NULL) |
| 193 |
#endif |
| 194 |
{ |
| 195 |
new_environ[size] = (char *) malloc (namelen + 1 + vallen); |
| 196 |
if (new_environ[size] == NULL) |
| 197 |
{ |
| 198 |
#if defined USE_TSEARCH && !defined _LIBC |
| 199 |
freea (new_value); |
| 200 |
#endif |
| 201 |
__set_errno (ENOMEM); |
| 202 |
UNLOCK; |
| 203 |
return -1; |
| 204 |
} |
| 205 |
|
| 206 |
#ifdef USE_TSEARCH |
| 207 |
memcpy (new_environ[size], new_value, namelen + 1 + vallen); |
| 208 |
#else |
| 209 |
memcpy (new_environ[size], name, namelen); |
| 210 |
new_environ[size][namelen] = '='; |
| 211 |
memcpy (&new_environ[size][namelen + 1], value, vallen); |
| 212 |
#endif |
| 213 |
/* And save the value now. We cannot do this when we remove |
| 214 |
the string since then we cannot decide whether it is a |
| 215 |
user string or not. */ |
| 216 |
STORE_VALUE (new_environ[size]); |
| 217 |
} |
| 218 |
#if defined USE_TSEARCH && !defined _LIBC |
| 219 |
freea (new_value); |
| 220 |
#endif |
| 221 |
} |
| 222 |
|
| 223 |
if (__environ != last_environ) |
| 224 |
memcpy (new_environ, __environ, size * sizeof (char *)); |
| 225 |
|
| 226 |
new_environ[size + 1] = NULL; |
| 227 |
|
| 228 |
last_environ = __environ = new_environ; |
| 229 |
} |
| 230 |
else if (replace) |
| 231 |
{ |
| 232 |
char *np; |
| 233 |
|
| 234 |
/* Use the user string if given. */ |
| 235 |
if (combined != NULL) |
| 236 |
np = (char *) combined; |
| 237 |
else |
| 238 |
{ |
| 239 |
#ifdef USE_TSEARCH |
| 240 |
char *new_value; |
| 241 |
# ifdef _LIBC |
| 242 |
new_value = alloca (namelen + 1 + vallen); |
| 243 |
__mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), |
| 244 |
value, vallen); |
| 245 |
# else |
| 246 |
new_value = malloca (namelen + 1 + vallen); |
| 247 |
if (new_value == NULL) |
| 248 |
{ |
| 249 |
__set_errno (ENOMEM); |
| 250 |
UNLOCK; |
| 251 |
return -1; |
| 252 |
} |
| 253 |
memcpy (new_value, name, namelen); |
| 254 |
new_value[namelen] = '='; |
| 255 |
memcpy (&new_value[namelen + 1], value, vallen); |
| 256 |
# endif |
| 257 |
|
| 258 |
np = KNOWN_VALUE (new_value); |
| 259 |
if (np == NULL) |
| 260 |
#endif |
| 261 |
{ |
| 262 |
np = (char *) malloc (namelen + 1 + vallen); |
| 263 |
if (np == NULL) |
| 264 |
{ |
| 265 |
#if defined USE_TSEARCH && !defined _LIBC |
| 266 |
freea (new_value); |
| 267 |
#endif |
| 268 |
__set_errno (ENOMEM); |
| 269 |
UNLOCK; |
| 270 |
return -1; |
| 271 |
} |
| 272 |
|
| 273 |
#ifdef USE_TSEARCH |
| 274 |
memcpy (np, new_value, namelen + 1 + vallen); |
| 275 |
#else |
| 276 |
memcpy (np, name, namelen); |
| 277 |
np[namelen] = '='; |
| 278 |
memcpy (&np[namelen + 1], value, vallen); |
| 279 |
#endif |
| 280 |
/* And remember the value. */ |
| 281 |
STORE_VALUE (np); |
| 282 |
} |
| 283 |
#if defined USE_TSEARCH && !defined _LIBC |
| 284 |
freea (new_value); |
| 285 |
#endif |
| 286 |
} |
| 287 |
|
| 288 |
*ep = np; |
| 289 |
} |
| 290 |
|
| 291 |
UNLOCK; |
| 292 |
|
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
int |
| 297 |
setenv (const char *name, const char *value, int replace) |
| 298 |
{ |
| 299 |
if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) |
| 300 |
{ |
| 301 |
__set_errno (EINVAL); |
| 302 |
return -1; |
| 303 |
} |
| 304 |
|
| 305 |
return __add_to_environ (name, value, NULL, replace); |
| 306 |
} |
| 307 |
|
| 308 |
/* The 'clearenv' was planned to be added to POSIX.1 but probably |
| 309 |
never made it. Nevertheless the POSIX.9 standard (POSIX bindings |
| 310 |
for Fortran 77) requires this function. */ |
| 311 |
int |
| 312 |
clearenv (void) |
| 313 |
{ |
| 314 |
LOCK; |
| 315 |
|
| 316 |
if (__environ == last_environ && __environ != NULL) |
| 317 |
{ |
| 318 |
/* We allocated this environment so we can free it. */ |
| 319 |
free (__environ); |
| 320 |
last_environ = NULL; |
| 321 |
} |
| 322 |
|
| 323 |
/* Clear the environment pointer removes the whole environment. */ |
| 324 |
__environ = NULL; |
| 325 |
|
| 326 |
UNLOCK; |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
#ifdef _LIBC |
| 332 |
static void |
| 333 |
free_mem (void) |
| 334 |
{ |
| 335 |
/* Remove all traces. */ |
| 336 |
clearenv (); |
| 337 |
|
| 338 |
/* Now remove the search tree. */ |
| 339 |
__tdestroy (known_values, free); |
| 340 |
known_values = NULL; |
| 341 |
} |
| 342 |
text_set_element (__libc_subfreeres, free_mem); |
| 343 |
|
| 344 |
|
| 345 |
# undef setenv |
| 346 |
# undef clearenv |
| 347 |
weak_alias (__setenv, setenv) |
| 348 |
weak_alias (__clearenv, clearenv) |
| 349 |
#endif |
| 350 |
|
| 351 |
#else /* HAVE_DECL__PUTENV */ |
| 352 |
/* Native Windows */ |
| 353 |
|
| 354 |
int |
| 355 |
setenv (const char *name, const char *value, int replace) |
| 356 |
{ |
| 357 |
if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) |
| 358 |
{ |
| 359 |
errno = EINVAL; |
| 360 |
return -1; |
| 361 |
} |
| 362 |
|
| 363 |
/* The Microsoft documentation |
| 364 |
<https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-wputenv> |
| 365 |
says: |
| 366 |
"Don't change an environment entry directly: instead, |
| 367 |
use _putenv or _wputenv to change it." |
| 368 |
Note: Microsoft's _putenv updates not only the contents of _environ but |
| 369 |
also the contents of _wenviron, so that both are in kept in sync. */ |
| 370 |
const char *existing_value = getenv (name); |
| 371 |
if (existing_value != NULL) |
| 372 |
{ |
| 373 |
if (replace) |
| 374 |
{ |
| 375 |
if (strcmp (existing_value, value) == 0) |
| 376 |
/* No need to allocate memory. */ |
| 377 |
return 0; |
| 378 |
} |
| 379 |
else |
| 380 |
/* Keep the existing value. */ |
| 381 |
return 0; |
| 382 |
} |
| 383 |
/* Allocate a new environment entry in the heap. */ |
| 384 |
/* _putenv ("NAME=") unsets NAME, so if VALUE is the empty string, invoke |
| 385 |
_putenv ("NAME= ") and fix up the result afterwards. */ |
| 386 |
const char *value_ = (value[0] == '\0' ? " " : value); |
| 387 |
size_t name_len = strlen (name); |
| 388 |
size_t value_len = strlen (value_); |
| 389 |
char *string = (char *) malloc (name_len + 1 + value_len + 1); |
| 390 |
if (string == NULL) |
| 391 |
return -1; |
| 392 |
memcpy (string, name, name_len); |
| 393 |
string[name_len] = '='; |
| 394 |
memcpy (&string[name_len + 1], value_, value_len + 1); |
| 395 |
/* Use _putenv. */ |
| 396 |
if (_putenv (string) < 0) |
| 397 |
return -1; |
| 398 |
if (value[0] == '\0') |
| 399 |
{ |
| 400 |
/* Fix up the result. */ |
| 401 |
char *new_value = getenv (name); |
| 402 |
if (new_value != NULL && new_value[0] == ' ' && new_value[1] == '\0') |
| 403 |
new_value[0] = '\0'; |
| 404 |
# if defined _WIN32 && ! defined __CYGWIN__ |
| 405 |
/* _putenv propagated "NAME= " into the subprocess environment; |
| 406 |
fix that by calling SetEnvironmentVariable directly. */ |
| 407 |
/* Documentation: |
| 408 |
<https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setenvironmentvariable> */ |
| 409 |
if (!SetEnvironmentVariable (name, "")) |
| 410 |
{ |
| 411 |
switch (GetLastError ()) |
| 412 |
{ |
| 413 |
case ERROR_NOT_ENOUGH_MEMORY: |
| 414 |
case ERROR_OUTOFMEMORY: |
| 415 |
errno = ENOMEM; |
| 416 |
break; |
| 417 |
default: |
| 418 |
errno = EINVAL; |
| 419 |
break; |
| 420 |
} |
| 421 |
return -1; |
| 422 |
} |
| 423 |
# endif |
| 424 |
} |
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
#endif /* HAVE_DECL__PUTENV */ |
| 429 |
#endif /* _LIBC || !HAVE_SETENV */ |
| 430 |
|
| 431 |
/* The rest of this file is called into use when replacing an existing |
| 432 |
but buggy setenv. Known bugs include failure to diagnose invalid |
| 433 |
name, and consuming a leading '=' from value. */ |
| 434 |
#if HAVE_SETENV |
| 435 |
|
| 436 |
# undef setenv |
| 437 |
# if !HAVE_DECL_SETENV |
| 438 |
extern int setenv (const char *, const char *, int); |
| 439 |
# endif |
| 440 |
# define STREQ(a, b) (strcmp (a, b) == 0) |
| 441 |
|
| 442 |
int |
| 443 |
rpl_setenv (const char *name, const char *value, int replace) |
| 444 |
{ |
| 445 |
if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) |
| 446 |
{ |
| 447 |
errno = EINVAL; |
| 448 |
return -1; |
| 449 |
} |
| 450 |
/* Call the real setenv even if replace is 0, in case implementation |
| 451 |
has underlying data to update, such as when environ changes. */ |
| 452 |
int result = setenv (name, value, replace); |
| 453 |
if (result == 0 && replace && *value == '=') |
| 454 |
{ |
| 455 |
char *tmp = getenv (name); |
| 456 |
if (!STREQ (tmp, value)) |
| 457 |
{ |
| 458 |
size_t len = strlen (value); |
| 459 |
tmp = malloca (len + 2); |
| 460 |
if (tmp == NULL) |
| 461 |
{ |
| 462 |
errno = ENOMEM; |
| 463 |
return -1; |
| 464 |
} |
| 465 |
/* Since leading '=' is eaten, double it up. */ |
| 466 |
*tmp = '='; |
| 467 |
memcpy (tmp + 1, value, len + 1); |
| 468 |
result = setenv (name, tmp, replace); |
| 469 |
int saved_errno = errno; |
| 470 |
freea (tmp); |
| 471 |
errno = saved_errno; |
| 472 |
} |
| 473 |
} |
| 474 |
return result; |
| 475 |
} |
| 476 |
|
| 477 |
#endif /* HAVE_SETENV */ |