setenv.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (C) 1992,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, see <http://www.gnu.org/licenses/>.
  14. modified for uClibc by Erik Andersen <andersen@codepoet.org>
  15. */
  16. #include <features.h>
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <bits/uClibc_mutex.h>
  22. __UCLIBC_MUTEX_STATIC(mylock, PTHREAD_MUTEX_INITIALIZER);
  23. /* If this variable is not a null pointer we allocated the current
  24. environment. */
  25. static char **last_environ;
  26. /* This function is used by `setenv' and `putenv'. The difference between
  27. the two functions is that for the former must create a new string which
  28. is then placed in the environment, while the argument of `putenv'
  29. must be used directly. This is all complicated by the fact that we try
  30. to reuse values once generated for a `setenv' call since we can never
  31. free the strings. [in uclibc, we do not] */
  32. static int __add_to_environ(const char *name, const char *value,
  33. int replace)
  34. {
  35. register char **ep;
  36. register size_t size;
  37. char *var_val;
  38. char **new_environ;
  39. /* name may come from putenv() and thus may contain "=VAL" part */
  40. const size_t namelen = strchrnul(name, '=') - name;
  41. int rv = -1;
  42. __UCLIBC_MUTEX_LOCK(mylock);
  43. /* We have to get the pointer now that we have the lock and not earlier
  44. since another thread might have created a new environment. */
  45. ep = __environ;
  46. size = 0;
  47. if (ep != NULL) {
  48. while (*ep != NULL) {
  49. if (!strncmp(*ep, name, namelen) && (*ep)[namelen] == '=') {
  50. /* Found */
  51. if (!replace)
  52. goto DONE_OK;
  53. goto REPLACE;
  54. }
  55. ++size;
  56. ++ep;
  57. }
  58. }
  59. /* Not found, add at the end */
  60. /* We allocated this space; we can extend it. */
  61. new_environ = realloc(last_environ, (size + 2) * sizeof(char *));
  62. if (new_environ == NULL) {
  63. __set_errno(ENOMEM);
  64. goto DONE;
  65. }
  66. if (__environ != last_environ) {
  67. memcpy(new_environ, __environ, size * sizeof(char *));
  68. }
  69. last_environ = __environ = new_environ;
  70. ep = &new_environ[size];
  71. /* Ensure env is NULL terminated in case malloc below fails */
  72. ep[0] = NULL;
  73. ep[1] = NULL;
  74. REPLACE:
  75. var_val = (char*) name;
  76. /* Build VAR=VAL if we called by setenv, not putenv. */
  77. if (value != NULL) {
  78. const size_t vallen = strlen(value) + 1;
  79. var_val = malloc(namelen + 1 + vallen);
  80. if (var_val == NULL) {
  81. __set_errno(ENOMEM);
  82. goto DONE;
  83. }
  84. memcpy(var_val, name, namelen);
  85. var_val[namelen] = '=';
  86. memcpy(&var_val[namelen + 1], value, vallen);
  87. }
  88. *ep = var_val;
  89. DONE_OK:
  90. rv = 0;
  91. DONE:
  92. __UCLIBC_MUTEX_UNLOCK(mylock);
  93. return rv;
  94. }
  95. int setenv(const char *name, const char *value, int replace)
  96. {
  97. /* NB: setenv("VAR", NULL, 1) inserts "VAR=" string */
  98. return __add_to_environ(name, value ? value : "", replace);
  99. }
  100. libc_hidden_def(setenv)
  101. int unsetenv(const char *name)
  102. {
  103. const char *eq;
  104. size_t len;
  105. char **ep;
  106. if (name == NULL || *name == '\0'
  107. || *(eq = strchrnul(name, '=')) == '='
  108. ) {
  109. __set_errno(EINVAL);
  110. return -1;
  111. }
  112. len = eq - name; /* avoiding strlen this way */
  113. __UCLIBC_MUTEX_LOCK(mylock);
  114. ep = __environ;
  115. /* NB: clearenv(); unsetenv("foo"); should not segfault */
  116. if (ep) while (*ep != NULL) {
  117. if (!strncmp(*ep, name, len) && (*ep)[len] == '=') {
  118. /* Found it. Remove this pointer by moving later ones back. */
  119. char **dp = ep;
  120. do {
  121. dp[0] = dp[1];
  122. } while (*dp++);
  123. /* Continue the loop in case NAME appears again. */
  124. } else {
  125. ++ep;
  126. }
  127. }
  128. __UCLIBC_MUTEX_UNLOCK(mylock);
  129. return 0;
  130. }
  131. libc_hidden_def(unsetenv)
  132. /* The `clearenv' was planned to be added to POSIX.1 but probably
  133. never made it. Nevertheless the POSIX.9 standard (POSIX bindings
  134. for Fortran 77) requires this function. */
  135. int clearenv(void)
  136. {
  137. __UCLIBC_MUTEX_LOCK(mylock);
  138. /* If we allocated this environment we can free it.
  139. * If we did not allocate this environment, it's NULL already
  140. * and is safe to free(). */
  141. free(last_environ);
  142. last_environ = NULL;
  143. /* Clearing environ removes the whole environment. */
  144. __environ = NULL;
  145. __UCLIBC_MUTEX_UNLOCK(mylock);
  146. return 0;
  147. }
  148. /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
  149. int putenv(char *string)
  150. {
  151. if (strchr(string, '=') != NULL) {
  152. return __add_to_environ(string, NULL, 1);
  153. }
  154. return unsetenv(string);
  155. }