putenv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2023 Free Software
  2. Foundation, Inc.
  3. NOTE: The canonical source of this file is maintained with the GNU C
  4. Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
  5. This file is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as
  7. published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  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. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  15. #include <config.h>
  16. /* Specification. */
  17. #include <stdlib.h>
  18. #include <stddef.h>
  19. /* Include errno.h *after* sys/types.h to work around header problems
  20. on AIX 3.2.5. */
  21. #include <errno.h>
  22. #ifndef __set_errno
  23. # define __set_errno(ev) ((errno) = (ev))
  24. #endif
  25. #include <string.h>
  26. #include <unistd.h>
  27. #if defined _WIN32 && ! defined __CYGWIN__
  28. # define WIN32_LEAN_AND_MEAN
  29. # include <windows.h>
  30. #endif
  31. #if _LIBC
  32. # if HAVE_GNU_LD
  33. # define environ __environ
  34. # else
  35. extern char **environ;
  36. # endif
  37. #endif
  38. #if _LIBC
  39. /* This lock protects against simultaneous modifications of 'environ'. */
  40. # include <bits/libc-lock.h>
  41. __libc_lock_define_initialized (static, envlock)
  42. # define LOCK __libc_lock_lock (envlock)
  43. # define UNLOCK __libc_lock_unlock (envlock)
  44. #else
  45. # define LOCK
  46. # define UNLOCK
  47. #endif
  48. #if defined _WIN32 && ! defined __CYGWIN__
  49. /* Don't assume that UNICODE is not defined. */
  50. # undef SetEnvironmentVariable
  51. # define SetEnvironmentVariable SetEnvironmentVariableA
  52. #endif
  53. static int
  54. _unsetenv (const char *name)
  55. {
  56. size_t len;
  57. #if !HAVE_DECL__PUTENV
  58. char **ep;
  59. #endif
  60. if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
  61. {
  62. __set_errno (EINVAL);
  63. return -1;
  64. }
  65. len = strlen (name);
  66. #if HAVE_DECL__PUTENV
  67. {
  68. int putenv_result;
  69. char *name_ = malloc (len + 2);
  70. memcpy (name_, name, len);
  71. name_[len] = '=';
  72. name_[len + 1] = 0;
  73. putenv_result = _putenv (name_);
  74. free (name_);
  75. return putenv_result;
  76. }
  77. #else
  78. LOCK;
  79. ep = environ;
  80. while (*ep != NULL)
  81. if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
  82. {
  83. /* Found it. Remove this pointer by moving later ones back. */
  84. char **dp = ep;
  85. do
  86. dp[0] = dp[1];
  87. while (*dp++);
  88. /* Continue the loop in case NAME appears again. */
  89. }
  90. else
  91. ++ep;
  92. UNLOCK;
  93. return 0;
  94. #endif
  95. }
  96. /* Put STRING, which is of the form "NAME=VALUE", in the environment.
  97. If STRING contains no '=', then remove STRING from the environment. */
  98. int
  99. putenv (char *string)
  100. {
  101. const char *name_end = strchr (string, '=');
  102. char **ep;
  103. if (name_end == NULL)
  104. {
  105. /* Remove the variable from the environment. */
  106. return _unsetenv (string);
  107. }
  108. #if HAVE_DECL__PUTENV
  109. /* Rely on _putenv to allocate the new environment. If other
  110. parts of the application use _putenv, the !HAVE_DECL__PUTENV code
  111. would fight over who owns the environ vector, causing a crash. */
  112. if (name_end[1])
  113. return _putenv (string);
  114. else
  115. {
  116. /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
  117. to allocate the environ vector and then replace the new
  118. entry with "NAME=". */
  119. int putenv_result;
  120. char *name_x = malloc (name_end - string + sizeof "= ");
  121. if (!name_x)
  122. return -1;
  123. memcpy (name_x, string, name_end - string + 1);
  124. name_x[name_end - string + 1] = ' ';
  125. name_x[name_end - string + 2] = 0;
  126. putenv_result = _putenv (name_x);
  127. for (ep = environ; *ep; ep++)
  128. if (strcmp (*ep, name_x) == 0)
  129. {
  130. *ep = string;
  131. break;
  132. }
  133. # if defined _WIN32 && ! defined __CYGWIN__
  134. if (putenv_result == 0)
  135. {
  136. /* _putenv propagated "NAME= " into the subprocess environment;
  137. fix that by calling SetEnvironmentVariable directly. */
  138. name_x[name_end - string] = 0;
  139. putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
  140. errno = ENOMEM; /* ENOMEM is the only way to fail. */
  141. }
  142. # endif
  143. free (name_x);
  144. return putenv_result;
  145. }
  146. #else
  147. for (ep = environ; *ep; ep++)
  148. if (strncmp (*ep, string, name_end - string) == 0
  149. && (*ep)[name_end - string] == '=')
  150. break;
  151. if (*ep)
  152. *ep = string;
  153. else
  154. {
  155. static char **last_environ = NULL;
  156. size_t size = ep - environ;
  157. char **new_environ = malloc ((size + 2) * sizeof *new_environ);
  158. if (! new_environ)
  159. return -1;
  160. new_environ[0] = string;
  161. memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
  162. free (last_environ);
  163. last_environ = new_environ;
  164. environ = new_environ;
  165. }
  166. return 0;
  167. #endif
  168. }