putenv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2021 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 program is free software: you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published by the
  7. Free Software Foundation; either version 3 of the License, or any
  8. later version.
  9. This program 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, putenv_errno;
  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. putenv_errno = errno;
  75. free (name_);
  76. __set_errno (putenv_errno);
  77. return putenv_result;
  78. }
  79. #else
  80. LOCK;
  81. ep = environ;
  82. while (*ep != NULL)
  83. if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
  84. {
  85. /* Found it. Remove this pointer by moving later ones back. */
  86. char **dp = ep;
  87. do
  88. dp[0] = dp[1];
  89. while (*dp++);
  90. /* Continue the loop in case NAME appears again. */
  91. }
  92. else
  93. ++ep;
  94. UNLOCK;
  95. return 0;
  96. #endif
  97. }
  98. /* Put STRING, which is of the form "NAME=VALUE", in the environment.
  99. If STRING contains no '=', then remove STRING from the environment. */
  100. int
  101. putenv (char *string)
  102. {
  103. const char *name_end = strchr (string, '=');
  104. char **ep;
  105. if (name_end == NULL)
  106. {
  107. /* Remove the variable from the environment. */
  108. return _unsetenv (string);
  109. }
  110. #if HAVE_DECL__PUTENV
  111. /* Rely on _putenv to allocate the new environment. If other
  112. parts of the application use _putenv, the !HAVE_DECL__PUTENV code
  113. would fight over who owns the environ vector, causing a crash. */
  114. if (name_end[1])
  115. return _putenv (string);
  116. else
  117. {
  118. /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
  119. to allocate the environ vector and then replace the new
  120. entry with "NAME=". */
  121. int putenv_result, putenv_errno;
  122. char *name_x = malloc (name_end - string + sizeof "= ");
  123. if (!name_x)
  124. return -1;
  125. memcpy (name_x, string, name_end - string + 1);
  126. name_x[name_end - string + 1] = ' ';
  127. name_x[name_end - string + 2] = 0;
  128. putenv_result = _putenv (name_x);
  129. putenv_errno = errno;
  130. for (ep = environ; *ep; ep++)
  131. if (strcmp (*ep, name_x) == 0)
  132. {
  133. *ep = string;
  134. break;
  135. }
  136. # if defined _WIN32 && ! defined __CYGWIN__
  137. if (putenv_result == 0)
  138. {
  139. /* _putenv propagated "NAME= " into the subprocess environment;
  140. fix that by calling SetEnvironmentVariable directly. */
  141. name_x[name_end - string] = 0;
  142. putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
  143. putenv_errno = ENOMEM; /* ENOMEM is the only way to fail. */
  144. }
  145. # endif
  146. free (name_x);
  147. __set_errno (putenv_errno);
  148. return putenv_result;
  149. }
  150. #else
  151. for (ep = environ; *ep; ep++)
  152. if (strncmp (*ep, string, name_end - string) == 0
  153. && (*ep)[name_end - string] == '=')
  154. break;
  155. if (*ep)
  156. *ep = string;
  157. else
  158. {
  159. static char **last_environ = NULL;
  160. size_t size = ep - environ;
  161. char **new_environ = malloc ((size + 2) * sizeof *new_environ);
  162. if (! new_environ)
  163. return -1;
  164. new_environ[0] = string;
  165. memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
  166. free (last_environ);
  167. last_environ = new_environ;
  168. environ = new_environ;
  169. }
  170. return 0;
  171. #endif
  172. }