putenv.c 3.2 KB

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