putenv.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2017 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 <http://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 __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. static int
  49. _unsetenv (const char *name)
  50. {
  51. size_t len;
  52. #if !HAVE_DECL__PUTENV
  53. char **ep;
  54. #endif
  55. if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
  56. {
  57. __set_errno (EINVAL);
  58. return -1;
  59. }
  60. len = strlen (name);
  61. #if HAVE_DECL__PUTENV
  62. {
  63. int putenv_result, putenv_errno;
  64. char *name_ = malloc (len + 2);
  65. memcpy (name_, name, len);
  66. name_[len] = '=';
  67. name_[len + 1] = 0;
  68. putenv_result = _putenv (name_);
  69. putenv_errno = errno;
  70. free (name_);
  71. __set_errno (putenv_errno);
  72. return putenv_result;
  73. }
  74. #else
  75. LOCK;
  76. ep = environ;
  77. while (*ep != NULL)
  78. if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
  79. {
  80. /* Found it. Remove this pointer by moving later ones back. */
  81. char **dp = ep;
  82. do
  83. dp[0] = dp[1];
  84. while (*dp++);
  85. /* Continue the loop in case NAME appears again. */
  86. }
  87. else
  88. ++ep;
  89. UNLOCK;
  90. return 0;
  91. #endif
  92. }
  93. /* Put STRING, which is of the form "NAME=VALUE", in the environment.
  94. If STRING contains no '=', then remove STRING from the environment. */
  95. int
  96. putenv (char *string)
  97. {
  98. const char *name_end = strchr (string, '=');
  99. char **ep;
  100. if (name_end == NULL)
  101. {
  102. /* Remove the variable from the environment. */
  103. return _unsetenv (string);
  104. }
  105. #if HAVE_DECL__PUTENV
  106. /* Rely on _putenv to allocate the new environment. If other
  107. parts of the application use _putenv, the !HAVE_DECL__PUTENV code
  108. would fight over who owns the environ vector, causing a crash. */
  109. if (name_end[1])
  110. return _putenv (string);
  111. else
  112. {
  113. /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
  114. to allocate the environ vector and then replace the new
  115. entry with "NAME=". */
  116. int putenv_result, putenv_errno;
  117. char *name_x = malloc (name_end - string + sizeof "= ");
  118. if (!name_x)
  119. return -1;
  120. memcpy (name_x, string, name_end - string + 1);
  121. name_x[name_end - string + 1] = ' ';
  122. name_x[name_end - string + 2] = 0;
  123. putenv_result = _putenv (name_x);
  124. putenv_errno = errno;
  125. for (ep = environ; *ep; ep++)
  126. if (strcmp (*ep, name_x) == 0)
  127. {
  128. *ep = string;
  129. break;
  130. }
  131. # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
  132. if (putenv_result == 0)
  133. {
  134. /* _putenv propagated "NAME= " into the subprocess environment;
  135. fix that by calling SetEnvironmentVariable directly. */
  136. name_x[name_end - string] = 0;
  137. putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
  138. putenv_errno = ENOMEM; /* ENOMEM is the only way to fail. */
  139. }
  140. # endif
  141. free (name_x);
  142. __set_errno (putenv_errno);
  143. return putenv_result;
  144. }
  145. #else
  146. for (ep = environ; *ep; ep++)
  147. if (strncmp (*ep, string, name_end - string) == 0
  148. && (*ep)[name_end - string] == '=')
  149. break;
  150. if (*ep)
  151. *ep = string;
  152. else
  153. {
  154. static char **last_environ = NULL;
  155. size_t size = ep - environ;
  156. char **new_environ = malloc ((size + 2) * sizeof *new_environ);
  157. if (! new_environ)
  158. return -1;
  159. new_environ[0] = string;
  160. memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
  161. free (last_environ);
  162. last_environ = new_environ;
  163. environ = new_environ;
  164. }
  165. return 0;
  166. #endif
  167. }