putenv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2011 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 HAVE_GNU_LD
  28. # define environ __environ
  29. #else
  30. extern char **environ;
  31. #endif
  32. #if _LIBC
  33. /* This lock protects against simultaneous modifications of `environ'. */
  34. # include <bits/libc-lock.h>
  35. __libc_lock_define_initialized (static, envlock)
  36. # define LOCK __libc_lock_lock (envlock)
  37. # define UNLOCK __libc_lock_unlock (envlock)
  38. #else
  39. # define LOCK
  40. # define UNLOCK
  41. #endif
  42. static int
  43. _unsetenv (const char *name)
  44. {
  45. size_t len;
  46. char **ep;
  47. if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
  48. {
  49. __set_errno (EINVAL);
  50. return -1;
  51. }
  52. len = strlen (name);
  53. LOCK;
  54. ep = environ;
  55. while (*ep != NULL)
  56. if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
  57. {
  58. /* Found it. Remove this pointer by moving later ones back. */
  59. char **dp = ep;
  60. do
  61. dp[0] = dp[1];
  62. while (*dp++);
  63. /* Continue the loop in case NAME appears again. */
  64. }
  65. else
  66. ++ep;
  67. UNLOCK;
  68. return 0;
  69. }
  70. /* Put STRING, which is of the form "NAME=VALUE", in the environment.
  71. If STRING contains no `=', then remove STRING from the environment. */
  72. int
  73. putenv (char *string)
  74. {
  75. const char *const name_end = strchr (string, '=');
  76. register size_t size;
  77. register char **ep;
  78. if (name_end == NULL)
  79. {
  80. /* Remove the variable from the environment. */
  81. return _unsetenv (string);
  82. }
  83. size = 0;
  84. for (ep = environ; *ep != NULL; ++ep)
  85. if (!strncmp (*ep, string, name_end - string) &&
  86. (*ep)[name_end - string] == '=')
  87. break;
  88. else
  89. ++size;
  90. if (*ep == NULL)
  91. {
  92. static char **last_environ = NULL;
  93. char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
  94. if (new_environ == NULL)
  95. return -1;
  96. (void) memcpy ((void *) new_environ, (void *) environ,
  97. size * sizeof (char *));
  98. new_environ[size] = (char *) string;
  99. new_environ[size + 1] = NULL;
  100. free (last_environ);
  101. last_environ = new_environ;
  102. environ = new_environ;
  103. }
  104. else
  105. *ep = string;
  106. return 0;
  107. }