setenv.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #define strndup __strndup
  18. #define _GNU_SOURCE
  19. #include <features.h>
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #ifdef __UCLIBC_HAS_THREADS__
  25. #include <pthread.h>
  26. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  27. # define LOCK __pthread_mutex_lock(&mylock)
  28. # define UNLOCK __pthread_mutex_unlock(&mylock);
  29. #else
  30. # define LOCK
  31. # define UNLOCK
  32. #endif
  33. extern int __unsetenv (__const char *__name) attribute_hidden;
  34. /* If this variable is not a null pointer we allocated the current
  35. environment. */
  36. static char **last_environ;
  37. /* This function is used by `setenv' and `putenv'. The difference between
  38. the two functions is that for the former must create a new string which
  39. is then placed in the environment, while the argument of `putenv'
  40. must be used directly. This is all complicated by the fact that we try
  41. to reuse values once generated for a `setenv' call since we can never
  42. free the strings. */
  43. int attribute_hidden __add_to_environ (const char *name, const char *value,
  44. const char *combined, int replace)
  45. {
  46. register char **ep;
  47. register size_t size;
  48. const size_t namelen = strlen (name);
  49. const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
  50. LOCK;
  51. /* We have to get the pointer now that we have the lock and not earlier
  52. since another thread might have created a new environment. */
  53. ep = __environ;
  54. size = 0;
  55. if (ep != NULL) {
  56. for (; *ep != NULL; ++ep) {
  57. if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
  58. break;
  59. else
  60. ++size;
  61. }
  62. }
  63. if (ep == NULL || *ep == NULL) {
  64. char **new_environ;
  65. /* We allocated this space; we can extend it. */
  66. new_environ = (char **) realloc (last_environ,
  67. (size + 2) * sizeof (char *));
  68. if (new_environ == NULL) {
  69. UNLOCK;
  70. return -1;
  71. }
  72. /* If the whole entry is given add it. */
  73. if (combined != NULL) {
  74. /* We must not add the string to the search tree since it belongs
  75. to the user. */
  76. new_environ[size] = (char *) combined;
  77. } else {
  78. /* See whether the value is already known. */
  79. new_environ[size] = (char *) malloc (namelen + 1 + vallen);
  80. if (new_environ[size] == NULL) {
  81. __set_errno (ENOMEM);
  82. UNLOCK;
  83. return -1;
  84. }
  85. memcpy (new_environ[size], name, namelen);
  86. new_environ[size][namelen] = '=';
  87. memcpy (&new_environ[size][namelen + 1], value, vallen);
  88. }
  89. if (__environ != last_environ) {
  90. memcpy ((char *) new_environ, (char *) __environ,
  91. size * sizeof (char *));
  92. }
  93. new_environ[size + 1] = NULL;
  94. last_environ = __environ = new_environ;
  95. } else if (replace) {
  96. char *np;
  97. /* Use the user string if given. */
  98. if (combined != NULL) {
  99. np = (char *) combined;
  100. } else {
  101. np = malloc (namelen + 1 + vallen);
  102. if (np == NULL) {
  103. UNLOCK;
  104. return -1;
  105. }
  106. memcpy (np, name, namelen);
  107. np[namelen] = '=';
  108. memcpy (&np[namelen + 1], value, vallen);
  109. }
  110. *ep = np;
  111. }
  112. UNLOCK;
  113. return 0;
  114. }
  115. int setenv (const char *name, const char *value, int replace)
  116. {
  117. return __add_to_environ (name, value, NULL, replace);
  118. }
  119. int attribute_hidden __unsetenv (const char *name)
  120. {
  121. size_t len;
  122. char **ep;
  123. if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) {
  124. __set_errno (EINVAL);
  125. return -1;
  126. }
  127. len = strlen (name);
  128. LOCK;
  129. ep = __environ;
  130. while (*ep != NULL) {
  131. if (!strncmp (*ep, name, len) && (*ep)[len] == '=') {
  132. /* Found it. Remove this pointer by moving later ones back. */
  133. char **dp = ep;
  134. do {
  135. dp[0] = dp[1];
  136. } while (*dp++);
  137. /* Continue the loop in case NAME appears again. */
  138. } else {
  139. ++ep;
  140. }
  141. }
  142. UNLOCK;
  143. return 0;
  144. }
  145. strong_alias(__unsetenv,unsetenv)
  146. /* The `clearenv' was planned to be added to POSIX.1 but probably
  147. never made it. Nevertheless the POSIX.9 standard (POSIX bindings
  148. for Fortran 77) requires this function. */
  149. int clearenv (void)
  150. {
  151. LOCK;
  152. if (__environ == last_environ && __environ != NULL) {
  153. /* We allocated this environment so we can free it. */
  154. free (__environ);
  155. last_environ = NULL;
  156. }
  157. /* Clear the environment pointer removes the whole environment. */
  158. __environ = NULL;
  159. UNLOCK;
  160. return 0;
  161. }
  162. /* Put STRING, which is of the form "NAME=VALUE", in the environment. */
  163. int putenv (char *string)
  164. {
  165. int result;
  166. const char *const name_end = strchr (string, '=');
  167. if (name_end != NULL) {
  168. char *name = strndup(string, name_end - string);
  169. result = __add_to_environ (name, NULL, string, 1);
  170. free(name);
  171. return(result);
  172. }
  173. __unsetenv (string);
  174. return 0;
  175. }