1 /* Copyright (C) 1992,1995-2001,2004 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,
16 see <http://www.gnu.org/licenses/>.
23 #include "assuan-defs.h"
25 #define __builtin_expect(cond,val) (cond)
29 # if !defined errno && !defined HAVE_ERRNO_DECL
32 # define __set_errno(ev) ((errno) = (ev))
35 #if _LIBC || HAVE_STDLIB_H
38 #if _LIBC || HAVE_STRING_H
41 #if _LIBC || HAVE_UNISTD_H
46 # define __environ environ
47 # ifndef HAVE_ENVIRON_DECL
48 extern char **environ
;
53 /* This lock protects against simultaneous modifications of `environ'. */
54 # include <bits/libc-lock.h>
55 __libc_lock_define_initialized (static, envlock
)
56 # define LOCK __libc_lock_lock (envlock)
57 # define UNLOCK __libc_lock_unlock (envlock)
63 /* In the GNU C library we must keep the namespace clean. */
65 # define setenv __setenv
66 # define unsetenv __unsetenv
67 # define clearenv __clearenv
68 # define tfind __tfind
69 # define tsearch __tsearch
72 /* In the GNU C library implementation we try to be more clever and
73 allow arbitrarily many changes of the environment given that the used
74 values are from a small set. Outside glibc this will eat up all
75 memory after a while. */
76 #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
78 # define USE_TSEARCH 1
81 /* This is a pointer to the root of the search tree with the known
83 static void *known_values
;
85 # define KNOWN_VALUE(Str) \
87 void *value = tfind (Str, &known_values, (__compar_fn_t) strcmp); \
88 value != NULL ? *(char **) value : NULL; \
90 # define STORE_VALUE(Str) \
91 tsearch (Str, &known_values, (__compar_fn_t) strcmp)
96 # define KNOWN_VALUE(Str) NULL
97 # define STORE_VALUE(Str) do { } while (0)
102 /* If this variable is not a null pointer we allocated the current
104 static char **last_environ
;
107 /* This function is used by `setenv' and `putenv'. The difference between
108 the two functions is that for the former must create a new string which
109 is then placed in the environment, while the argument of `putenv'
110 must be used directly. This is all complicated by the fact that we try
111 to reuse values once generated for a `setenv' call since we can never
114 __add_to_environ (const char *name
, const char *value
, const char *combined
,
118 register size_t size
;
119 const size_t namelen
= strlen (name
);
120 const size_t vallen
= value
!= NULL
? strlen (value
) + 1 : 0;
124 /* We have to get the pointer now that we have the lock and not earlier
125 since another thread might have created a new environment. */
131 for (; *ep
!= NULL
; ++ep
)
132 if (!strncmp (*ep
, name
, namelen
) && (*ep
)[namelen
] == '=')
138 if (ep
== NULL
|| __builtin_expect (*ep
== NULL
, 1))
142 /* We allocated this space; we can extend it. */
143 new_environ
= (char **) realloc (last_environ
,
144 (size
+ 2) * sizeof (char *));
145 if (new_environ
== NULL
)
151 /* If the whole entry is given add it. */
152 if (combined
!= NULL
)
153 /* We must not add the string to the search tree since it belongs
155 new_environ
[size
] = (char *) combined
;
158 /* See whether the value is already known. */
161 char new_value
[namelen
+ 1 + vallen
];
163 char *new_value
= (char *) alloca (namelen
+ 1 + vallen
);
166 __mempcpy (__mempcpy (__mempcpy (new_value
, name
, namelen
), "=", 1),
169 memcpy (new_value
, name
, namelen
);
170 new_value
[namelen
] = '=';
171 memcpy (&new_value
[namelen
+ 1], value
, vallen
);
174 new_environ
[size
] = KNOWN_VALUE (new_value
);
175 if (__builtin_expect (new_environ
[size
] == NULL
, 1))
178 new_environ
[size
] = (char *) malloc (namelen
+ 1 + vallen
);
179 if (__builtin_expect (new_environ
[size
] == NULL
, 0))
181 __set_errno (ENOMEM
);
187 memcpy (new_environ
[size
], new_value
, namelen
+ 1 + vallen
);
189 memcpy (new_environ
[size
], name
, namelen
);
190 new_environ
[size
][namelen
] = '=';
191 memcpy (&new_environ
[size
][namelen
+ 1], value
, vallen
);
193 /* And save the value now. We cannot do this when we remove
194 the string since then we cannot decide whether it is a
195 user string or not. */
196 STORE_VALUE (new_environ
[size
]);
200 if (__environ
!= last_environ
)
201 memcpy ((char *) new_environ
, (char *) __environ
,
202 size
* sizeof (char *));
204 new_environ
[size
+ 1] = NULL
;
206 last_environ
= __environ
= new_environ
;
212 /* Use the user string if given. */
213 if (combined
!= NULL
)
214 np
= (char *) combined
;
219 char new_value
[namelen
+ 1 + vallen
];
221 char *new_value
= (char *) alloca (namelen
+ 1 + vallen
);
224 __mempcpy (__mempcpy (__mempcpy (new_value
, name
, namelen
), "=", 1),
227 memcpy (new_value
, name
, namelen
);
228 new_value
[namelen
] = '=';
229 memcpy (&new_value
[namelen
+ 1], value
, vallen
);
232 np
= KNOWN_VALUE (new_value
);
233 if (__builtin_expect (np
== NULL
, 1))
236 np
= malloc (namelen
+ 1 + vallen
);
237 if (__builtin_expect (np
== NULL
, 0))
244 memcpy (np
, new_value
, namelen
+ 1 + vallen
);
246 memcpy (np
, name
, namelen
);
248 memcpy (&np
[namelen
+ 1], value
, vallen
);
250 /* And remember the value. */
264 setenv (const char *name
, const char *value
, int replace
)
266 if (name
== NULL
|| *name
== '\0' || strchr (name
, '=') != NULL
)
268 __set_errno (EINVAL
);
272 return __add_to_environ (name
, value
, NULL
, replace
);
276 unsetenv (const char *name
)
281 if (name
== NULL
|| *name
== '\0' || strchr (name
, '=') != NULL
)
283 __set_errno (EINVAL
);
293 if (!strncmp (*ep
, name
, len
) && (*ep
)[len
] == '=')
295 /* Found it. Remove this pointer by moving later ones back. */
301 /* Continue the loop in case NAME appears again. */
311 /* The `clearenv' was planned to be added to POSIX.1 but probably
312 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
313 for Fortran 77) requires this function. */
319 if (__environ
== last_environ
&& __environ
!= NULL
)
321 /* We allocated this environment so we can free it. */
326 /* Clear the environment pointer removes the whole environment. */
334 libc_freeres_fn (free_mem
)
336 /* Remove all traces. */
339 /* Now remove the search tree. */
340 __tdestroy (known_values
, free
);
347 weak_alias (__setenv
, setenv
)
348 weak_alias (__unsetenv
, unsetenv
)
349 weak_alias (__clearenv
, clearenv
)