1 /* Copyright (C) 1992-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
24 # if !defined errno && !defined HAVE_ERRNO_DECL
27 # define __set_errno(ev) ((errno) = (ev))
30 #if _LIBC || HAVE_STDLIB_H
33 #if _LIBC || HAVE_STRING_H
36 #if _LIBC || HAVE_UNISTD_H
41 # define __environ environ
42 # ifndef HAVE_ENVIRON_DECL
43 extern char **environ
;
48 /* This lock protects against simultaneous modifications of `environ'. */
49 # include <bits/libc-lock.h>
50 __libc_lock_define_initialized (static, envlock
)
51 # define LOCK __libc_lock_lock (envlock)
52 # define UNLOCK __libc_lock_unlock (envlock)
58 /* In the GNU C library we must keep the namespace clean. */
60 # define setenv __setenv
61 # define unsetenv __unsetenv
62 # define clearenv __clearenv
63 # define tfind __tfind
64 # define tsearch __tsearch
67 /* In the GNU C library implementation we try to be more clever and
68 allow arbitrarily many changes of the environment given that the used
69 values are from a small set. Outside glibc this will eat up all
70 memory after a while. */
71 #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
73 # define USE_TSEARCH 1
76 /* This is a pointer to the root of the search tree with the known
78 static void *known_values
;
80 # define KNOWN_VALUE(Str) \
82 void *value = tfind (Str, &known_values, (__compar_fn_t) strcmp); \
83 value != NULL ? *(char **) value : NULL; \
85 # define STORE_VALUE(Str) \
86 tsearch (Str, &known_values, (__compar_fn_t) strcmp)
91 # define KNOWN_VALUE(Str) NULL
92 # define STORE_VALUE(Str) do { } while (0)
97 /* If this variable is not a null pointer we allocated the current
99 static char **last_environ
;
102 /* This function is used by `setenv' and `putenv'. The difference between
103 the two functions is that for the former must create a new string which
104 is then placed in the environment, while the argument of `putenv'
105 must be used directly. This is all complicated by the fact that we try
106 to reuse values once generated for a `setenv' call since we can never
109 __add_to_environ (name
, value
, combined
, replace
)
112 const char *combined
;
117 const size_t namelen
= strlen (name
);
118 const size_t vallen
= value
!= NULL
? strlen (value
) + 1 : 0;
122 /* We have to get the pointer now that we have the lock and not earlier
123 since another thread might have created a new environment. */
129 for (; *ep
!= NULL
; ++ep
)
130 if (!strncmp (*ep
, name
, namelen
) && (*ep
)[namelen
] == '=')
136 if (ep
== NULL
|| __builtin_expect (*ep
== NULL
, 1))
140 /* We allocated this space; we can extend it. */
141 new_environ
= (char **) realloc (last_environ
,
142 (size
+ 2) * sizeof (char *));
143 if (new_environ
== NULL
)
149 if (__environ
!= last_environ
)
150 memcpy ((char *) new_environ
, (char *) __environ
,
151 size
* sizeof (char *));
153 new_environ
[size
] = NULL
;
154 new_environ
[size
+ 1] = NULL
;
155 ep
= new_environ
+ size
;
157 last_environ
= __environ
= new_environ
;
159 if (*ep
== NULL
|| replace
)
163 /* Use the user string if given. */
164 if (combined
!= NULL
)
165 np
= (char *) combined
;
168 const size_t varlen
= namelen
+ 1 + vallen
;
171 int use_alloca
= __libc_use_alloca (varlen
);
172 if (__builtin_expect (use_alloca
, 1))
173 new_value
= (char *) alloca (varlen
);
176 new_value
= malloc (varlen
);
177 if (new_value
== NULL
)
184 __mempcpy (__mempcpy (__mempcpy (new_value
, name
, namelen
), "=", 1),
187 memcpy (new_value
, name
, namelen
);
188 new_value
[namelen
] = '=';
189 memcpy (&new_value
[namelen
+ 1], value
, vallen
);
192 np
= KNOWN_VALUE (new_value
);
193 if (__glibc_likely (np
== NULL
))
197 if (__glibc_unlikely (! use_alloca
))
202 np
= malloc (varlen
);
203 if (__glibc_unlikely (np
== NULL
))
210 memcpy (np
, new_value
, varlen
);
212 memcpy (np
, name
, namelen
);
214 memcpy (&np
[namelen
+ 1], value
, vallen
);
217 /* And remember the value. */
231 setenv (name
, value
, replace
)
236 if (name
== NULL
|| *name
== '\0' || strchr (name
, '=') != NULL
)
238 __set_errno (EINVAL
);
242 return __add_to_environ (name
, value
, NULL
, replace
);
252 if (name
== NULL
|| *name
== '\0' || strchr (name
, '=') != NULL
)
254 __set_errno (EINVAL
);
265 if (!strncmp (*ep
, name
, len
) && (*ep
)[len
] == '=')
267 /* Found it. Remove this pointer by moving later ones back. */
273 /* Continue the loop in case NAME appears again. */
283 /* The `clearenv' was planned to be added to POSIX.1 but probably
284 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
285 for Fortran 77) requires this function. */
291 if (__environ
== last_environ
&& __environ
!= NULL
)
293 /* We allocated this environment so we can free it. */
298 /* Clear the environment pointer removes the whole environment. */
306 libc_freeres_fn (free_mem
)
308 /* Remove all traces. */
311 /* Now remove the search tree. */
312 __tdestroy (known_values
, free
);
319 weak_alias (__setenv
, setenv
)
320 weak_alias (__unsetenv
, unsetenv
)
321 weak_alias (__clearenv
, clearenv
)