setenv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* Copyright (C) 1992,1995-1999,2000-2003,2005-2008 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #if !_LIBC
  14. # include <config.h>
  15. #endif
  16. #include <alloca.h>
  17. /* Specification. */
  18. #include <stdlib.h>
  19. #include <errno.h>
  20. #ifndef __set_errno
  21. # define __set_errno(ev) ((errno) = (ev))
  22. #endif
  23. #include <string.h>
  24. #if _LIBC || HAVE_UNISTD_H
  25. # include <unistd.h>
  26. #endif
  27. #if _LIBC || !HAVE_SETENV
  28. #if !_LIBC
  29. # include "malloca.h"
  30. #endif
  31. #if !_LIBC
  32. # define __environ environ
  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. /* In the GNU C library we must keep the namespace clean. */
  45. #ifdef _LIBC
  46. # define setenv __setenv
  47. # define clearenv __clearenv
  48. # define tfind __tfind
  49. # define tsearch __tsearch
  50. #endif
  51. /* In the GNU C library implementation we try to be more clever and
  52. allow arbitrarily many changes of the environment given that the used
  53. values are from a small set. Outside glibc this will eat up all
  54. memory after a while. */
  55. #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
  56. && defined __GNUC__)
  57. # define USE_TSEARCH 1
  58. # include <search.h>
  59. typedef int (*compar_fn_t) (const void *, const void *);
  60. /* This is a pointer to the root of the search tree with the known
  61. values. */
  62. static void *known_values;
  63. # define KNOWN_VALUE(Str) \
  64. ({ \
  65. void *value = tfind (Str, &known_values, (compar_fn_t) strcmp); \
  66. value != NULL ? *(char **) value : NULL; \
  67. })
  68. # define STORE_VALUE(Str) \
  69. tsearch (Str, &known_values, (compar_fn_t) strcmp)
  70. #else
  71. # undef USE_TSEARCH
  72. # define KNOWN_VALUE(Str) NULL
  73. # define STORE_VALUE(Str) do { } while (0)
  74. #endif
  75. /* If this variable is not a null pointer we allocated the current
  76. environment. */
  77. static char **last_environ;
  78. /* This function is used by `setenv' and `putenv'. The difference between
  79. the two functions is that for the former must create a new string which
  80. is then placed in the environment, while the argument of `putenv'
  81. must be used directly. This is all complicated by the fact that we try
  82. to reuse values once generated for a `setenv' call since we can never
  83. free the strings. */
  84. int
  85. __add_to_environ (const char *name, const char *value, const char *combined,
  86. int replace)
  87. {
  88. register char **ep;
  89. register size_t size;
  90. const size_t namelen = strlen (name);
  91. const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
  92. LOCK;
  93. /* We have to get the pointer now that we have the lock and not earlier
  94. since another thread might have created a new environment. */
  95. ep = __environ;
  96. size = 0;
  97. if (ep != NULL)
  98. {
  99. for (; *ep != NULL; ++ep)
  100. if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
  101. break;
  102. else
  103. ++size;
  104. }
  105. if (ep == NULL || *ep == NULL)
  106. {
  107. char **new_environ;
  108. #ifdef USE_TSEARCH
  109. char *new_value;
  110. #endif
  111. /* We allocated this space; we can extend it. */
  112. new_environ =
  113. (char **) (last_environ == NULL
  114. ? malloc ((size + 2) * sizeof (char *))
  115. : realloc (last_environ, (size + 2) * sizeof (char *)));
  116. if (new_environ == NULL)
  117. {
  118. UNLOCK;
  119. return -1;
  120. }
  121. /* If the whole entry is given add it. */
  122. if (combined != NULL)
  123. /* We must not add the string to the search tree since it belongs
  124. to the user. */
  125. new_environ[size] = (char *) combined;
  126. else
  127. {
  128. /* See whether the value is already known. */
  129. #ifdef USE_TSEARCH
  130. # ifdef _LIBC
  131. new_value = (char *) alloca (namelen + 1 + vallen);
  132. __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
  133. value, vallen);
  134. # else
  135. new_value = (char *) malloca (namelen + 1 + vallen);
  136. if (new_value == NULL)
  137. {
  138. __set_errno (ENOMEM);
  139. UNLOCK;
  140. return -1;
  141. }
  142. memcpy (new_value, name, namelen);
  143. new_value[namelen] = '=';
  144. memcpy (&new_value[namelen + 1], value, vallen);
  145. # endif
  146. new_environ[size] = KNOWN_VALUE (new_value);
  147. if (new_environ[size] == NULL)
  148. #endif
  149. {
  150. new_environ[size] = (char *) malloc (namelen + 1 + vallen);
  151. if (new_environ[size] == NULL)
  152. {
  153. #if defined USE_TSEARCH && !defined _LIBC
  154. freea (new_value);
  155. #endif
  156. __set_errno (ENOMEM);
  157. UNLOCK;
  158. return -1;
  159. }
  160. #ifdef USE_TSEARCH
  161. memcpy (new_environ[size], new_value, namelen + 1 + vallen);
  162. #else
  163. memcpy (new_environ[size], name, namelen);
  164. new_environ[size][namelen] = '=';
  165. memcpy (&new_environ[size][namelen + 1], value, vallen);
  166. #endif
  167. /* And save the value now. We cannot do this when we remove
  168. the string since then we cannot decide whether it is a
  169. user string or not. */
  170. STORE_VALUE (new_environ[size]);
  171. }
  172. #if defined USE_TSEARCH && !defined _LIBC
  173. freea (new_value);
  174. #endif
  175. }
  176. if (__environ != last_environ)
  177. memcpy ((char *) new_environ, (char *) __environ,
  178. size * sizeof (char *));
  179. new_environ[size + 1] = NULL;
  180. last_environ = __environ = new_environ;
  181. }
  182. else if (replace)
  183. {
  184. char *np;
  185. /* Use the user string if given. */
  186. if (combined != NULL)
  187. np = (char *) combined;
  188. else
  189. {
  190. #ifdef USE_TSEARCH
  191. char *new_value;
  192. # ifdef _LIBC
  193. new_value = alloca (namelen + 1 + vallen);
  194. __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
  195. value, vallen);
  196. # else
  197. new_value = malloca (namelen + 1 + vallen);
  198. if (new_value == NULL)
  199. {
  200. __set_errno (ENOMEM);
  201. UNLOCK;
  202. return -1;
  203. }
  204. memcpy (new_value, name, namelen);
  205. new_value[namelen] = '=';
  206. memcpy (&new_value[namelen + 1], value, vallen);
  207. # endif
  208. np = KNOWN_VALUE (new_value);
  209. if (np == NULL)
  210. #endif
  211. {
  212. np = malloc (namelen + 1 + vallen);
  213. if (np == NULL)
  214. {
  215. #if defined USE_TSEARCH && !defined _LIBC
  216. freea (new_value);
  217. #endif
  218. __set_errno (ENOMEM);
  219. UNLOCK;
  220. return -1;
  221. }
  222. #ifdef USE_TSEARCH
  223. memcpy (np, new_value, namelen + 1 + vallen);
  224. #else
  225. memcpy (np, name, namelen);
  226. np[namelen] = '=';
  227. memcpy (&np[namelen + 1], value, vallen);
  228. #endif
  229. /* And remember the value. */
  230. STORE_VALUE (np);
  231. }
  232. #if defined USE_TSEARCH && !defined _LIBC
  233. freea (new_value);
  234. #endif
  235. }
  236. *ep = np;
  237. }
  238. UNLOCK;
  239. return 0;
  240. }
  241. int
  242. setenv (const char *name, const char *value, int replace)
  243. {
  244. return __add_to_environ (name, value, NULL, replace);
  245. }
  246. /* The `clearenv' was planned to be added to POSIX.1 but probably
  247. never made it. Nevertheless the POSIX.9 standard (POSIX bindings
  248. for Fortran 77) requires this function. */
  249. int
  250. clearenv (void)
  251. {
  252. LOCK;
  253. if (__environ == last_environ && __environ != NULL)
  254. {
  255. /* We allocated this environment so we can free it. */
  256. free (__environ);
  257. last_environ = NULL;
  258. }
  259. /* Clear the environment pointer removes the whole environment. */
  260. __environ = NULL;
  261. UNLOCK;
  262. return 0;
  263. }
  264. #ifdef _LIBC
  265. static void
  266. free_mem (void)
  267. {
  268. /* Remove all traces. */
  269. clearenv ();
  270. /* Now remove the search tree. */
  271. __tdestroy (known_values, free);
  272. known_values = NULL;
  273. }
  274. text_set_element (__libc_subfreeres, free_mem);
  275. # undef setenv
  276. # undef clearenv
  277. weak_alias (__setenv, setenv)
  278. weak_alias (__clearenv, clearenv)
  279. #endif
  280. #endif /* _LIBC || !HAVE_SETENV */