setenv.c 4.9 KB

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