]> git.ipfire.org Git - thirdparty/glibc.git/blame - stdlib/setenv.c
Use glibc_likely instead __builtin_expect.
[thirdparty/glibc.git] / stdlib / setenv.c
CommitLineData
d4697bc9 1/* Copyright (C) 1992-2014 Free Software Foundation, Inc.
f0e44959 2 This file is part of the GNU C Library.
28f540f4 3
f0e44959 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
28f540f4 8
f0e44959
UD
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
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4 17
f0e44959
UD
18#if HAVE_CONFIG_H
19# include <config.h>
196980f5
RM
20#endif
21
22#include <errno.h>
61c162b5 23#if !_LIBC
ff152e3f 24# if !defined errno && !defined HAVE_ERRNO_DECL
61c162b5
UD
25extern int errno;
26# endif
27# define __set_errno(ev) ((errno) = (ev))
28#endif
196980f5
RM
29
30#if _LIBC || HAVE_STDLIB_H
f0e44959 31# include <stdlib.h>
196980f5
RM
32#endif
33#if _LIBC || HAVE_STRING_H
f0e44959 34# include <string.h>
196980f5
RM
35#endif
36#if _LIBC || HAVE_UNISTD_H
f0e44959 37# include <unistd.h>
196980f5 38#endif
28f540f4 39
61c162b5 40#if !_LIBC
f0e44959 41# define __environ environ
61c162b5
UD
42# ifndef HAVE_ENVIRON_DECL
43extern char **environ;
44# endif
28f540f4
RM
45#endif
46
61c162b5 47#if _LIBC
9e0f4072 48/* This lock protects against simultaneous modifications of `environ'. */
5107cf1d 49# include <bits/libc-lock.h>
9e0f4072 50__libc_lock_define_initialized (static, envlock)
f0e44959
UD
51# define LOCK __libc_lock_lock (envlock)
52# define UNLOCK __libc_lock_unlock (envlock)
9e0f4072 53#else
f0e44959
UD
54# define LOCK
55# define UNLOCK
9e0f4072
RM
56#endif
57
3e5f5557
UD
58/* In the GNU C library we must keep the namespace clean. */
59#ifdef _LIBC
ff152e3f
UD
60# define setenv __setenv
61# define unsetenv __unsetenv
3e5f5557 62# define clearenv __clearenv
b17277cf
UD
63# define tfind __tfind
64# define tsearch __tsearch
3e5f5557
UD
65#endif
66
ff152e3f 67/* In the GNU C library implementation we try to be more clever and
49c091e5 68 allow arbitrarily many changes of the environment given that the used
ff152e3f
UD
69 values are from a small set. Outside glibc this will eat up all
70 memory after a while. */
a709dd43
UD
71#if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
72 && defined __GNUC__)
ff152e3f
UD
73# define USE_TSEARCH 1
74# include <search.h>
75
76/* This is a pointer to the root of the search tree with the known
77 values. */
78static void *known_values;
79
b17277cf
UD
80# define KNOWN_VALUE(Str) \
81 ({ \
a709dd43
UD
82 void *value = tfind (Str, &known_values, (__compar_fn_t) strcmp); \
83 value != NULL ? *(char **) value : NULL; \
b17277cf
UD
84 })
85# define STORE_VALUE(Str) \
a709dd43 86 tsearch (Str, &known_values, (__compar_fn_t) strcmp)
ff152e3f
UD
87
88#else
89# undef USE_TSEARCH
90
91# define KNOWN_VALUE(Str) NULL
92# define STORE_VALUE(Str) do { } while (0)
93
94#endif
95
3e5f5557 96
f0e44959
UD
97/* If this variable is not a null pointer we allocated the current
98 environment. */
99static char **last_environ;
100
101
de4a40d0
UD
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
107 free the strings. */
28f540f4 108int
de4a40d0 109__add_to_environ (name, value, combined, replace)
196980f5
RM
110 const char *name;
111 const char *value;
de4a40d0 112 const char *combined;
196980f5 113 int replace;
28f540f4 114{
2e09a79a
JM
115 char **ep;
116 size_t size;
196980f5 117 const size_t namelen = strlen (name);
de4a40d0 118 const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
28f540f4 119
9e0f4072
RM
120 LOCK;
121
7195db12
UD
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. */
124 ep = __environ;
125
28f540f4 126 size = 0;
96ff4937 127 if (ep != NULL)
b0772d3b 128 {
96ff4937 129 for (; *ep != NULL; ++ep)
b0772d3b
UD
130 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
131 break;
132 else
133 ++size;
134 }
196980f5 135
3504e5a6 136 if (ep == NULL || __builtin_expect (*ep == NULL, 1))
28f540f4 137 {
c63bfa79 138 const size_t varlen = namelen + 1 + vallen;
196980f5 139 char **new_environ;
86187531 140
ff152e3f
UD
141 /* We allocated this space; we can extend it. */
142 new_environ = (char **) realloc (last_environ,
143 (size + 2) * sizeof (char *));
28f540f4 144 if (new_environ == NULL)
9e0f4072
RM
145 {
146 UNLOCK;
147 return -1;
148 }
1448f324
OB
149
150 if (__environ != last_environ)
151 memcpy ((char *) new_environ, (char *) __environ,
152 size * sizeof (char *));
153
f3d338c9 154 new_environ[size] = NULL;
28f540f4 155 new_environ[size + 1] = NULL;
f3d338c9 156 ep = new_environ + size;
28f540f4 157
196980f5 158 last_environ = __environ = new_environ;
28f540f4 159 }
f3d338c9 160 if (*ep == NULL || replace)
28f540f4 161 {
cb37d842 162 char *np;
ff152e3f 163
de4a40d0
UD
164 /* Use the user string if given. */
165 if (combined != NULL)
166 np = (char *) combined;
167 else
168 {
c63bfa79 169 const size_t varlen = namelen + 1 + vallen;
ff152e3f 170#ifdef USE_TSEARCH
c63bfa79
UD
171 char *new_value;
172 int use_alloca = __libc_use_alloca (varlen);
173 if (__builtin_expect (use_alloca, 1))
174 new_value = (char *) alloca (varlen);
175 else
176 {
177 new_value = malloc (varlen);
178 if (new_value == NULL)
179 {
180 UNLOCK;
181 return -1;
182 }
183 }
ff152e3f 184# ifdef _LIBC
de4a40d0
UD
185 __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
186 value, vallen);
ff152e3f 187# else
de4a40d0
UD
188 memcpy (new_value, name, namelen);
189 new_value[namelen] = '=';
190 memcpy (&new_value[namelen + 1], value, vallen);
ff152e3f
UD
191# endif
192
de4a40d0 193 np = KNOWN_VALUE (new_value);
a1ffb40e 194 if (__glibc_likely (np == NULL))
de4a40d0 195#endif
9e0f4072 196 {
c63bfa79 197#ifdef USE_TSEARCH
a1ffb40e 198 if (__glibc_unlikely (! use_alloca))
c63bfa79
UD
199 np = new_value;
200 else
201#endif
de4a40d0 202 {
c63bfa79 203 np = malloc (varlen);
a1ffb40e 204 if (__glibc_unlikely (np == NULL))
c63bfa79
UD
205 {
206 UNLOCK;
207 return -1;
208 }
ff152e3f
UD
209
210#ifdef USE_TSEARCH
c63bfa79 211 memcpy (np, new_value, varlen);
86187531 212#else
c63bfa79
UD
213 memcpy (np, name, namelen);
214 np[namelen] = '=';
215 memcpy (&np[namelen + 1], value, vallen);
86187531 216#endif
c63bfa79 217 }
de4a40d0
UD
218 /* And remember the value. */
219 STORE_VALUE (np);
220 }
28f540f4 221 }
cb37d842 222
cb37d842 223 *ep = np;
28f540f4
RM
224 }
225
9e0f4072
RM
226 UNLOCK;
227
28f540f4
RM
228 return 0;
229}
196980f5 230
de4a40d0
UD
231int
232setenv (name, value, replace)
233 const char *name;
234 const char *value;
235 int replace;
236{
e17f8b61
RM
237 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
238 {
239 __set_errno (EINVAL);
240 return -1;
241 }
242
de4a40d0
UD
243 return __add_to_environ (name, value, NULL, replace);
244}
245
61f9d0a3 246int
299a95b9
RM
247unsetenv (name)
248 const char *name;
196980f5 249{
7c5bb945 250 size_t len;
196980f5
RM
251 char **ep;
252
61f9d0a3
UD
253 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
254 {
255 __set_errno (EINVAL);
256 return -1;
257 }
258
259 len = strlen (name);
260
9e0f4072
RM
261 LOCK;
262
de4a40d0 263 ep = __environ;
1fa7ae05
UD
264 if (ep != NULL)
265 while (*ep != NULL)
266 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
267 {
268 /* Found it. Remove this pointer by moving later ones back. */
269 char **dp = ep;
270
271 do
272 dp[0] = dp[1];
273 while (*dp++);
274 /* Continue the loop in case NAME appears again. */
275 }
276 else
277 ++ep;
9e0f4072
RM
278
279 UNLOCK;
61f9d0a3
UD
280
281 return 0;
196980f5 282}
f0e44959
UD
283
284/* The `clearenv' was planned to be added to POSIX.1 but probably
285 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
286 for Fortran 77) requires this function. */
287int
60d2f8f3 288clearenv (void)
f0e44959
UD
289{
290 LOCK;
291
292 if (__environ == last_environ && __environ != NULL)
293 {
de4a40d0 294 /* We allocated this environment so we can free it. */
f0e44959
UD
295 free (__environ);
296 last_environ = NULL;
297 }
298
299 /* Clear the environment pointer removes the whole environment. */
300 __environ = NULL;
301
302 UNLOCK;
303
304 return 0;
305}
3e5f5557 306#ifdef _LIBC
c877418f 307libc_freeres_fn (free_mem)
69071b2a
UD
308{
309 /* Remove all traces. */
310 clearenv ();
311
312 /* Now remove the search tree. */
313 __tdestroy (known_values, free);
314 known_values = NULL;
315}
69071b2a 316
ff152e3f
UD
317# undef setenv
318# undef unsetenv
3e5f5557 319# undef clearenv
ff152e3f
UD
320weak_alias (__setenv, setenv)
321weak_alias (__unsetenv, unsetenv)
3e5f5557
UD
322weak_alias (__clearenv, clearenv)
323#endif