xref: /netbsd-src/external/gpl2/xcvs/dist/lib/setenv.c (revision 5a6c14c844c4c665da5632061aebde7bb2cb5766)
1a7c91847Schristos /* Copyright (C) 1992,1995-1999,2000-2003 Free Software Foundation, Inc.
2a7c91847Schristos    This file is part of the GNU C Library.
3a7c91847Schristos 
4a7c91847Schristos    This program is free software; you can redistribute it and/or modify
5a7c91847Schristos    it under the terms of the GNU General Public License as published by
6a7c91847Schristos    the Free Software Foundation; either version 2, or (at your option)
7a7c91847Schristos    any later version.
8a7c91847Schristos 
9a7c91847Schristos    This program is distributed in the hope that it will be useful,
10a7c91847Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
11a7c91847Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12a7c91847Schristos    GNU General Public License for more details.
13a7c91847Schristos 
14a7c91847Schristos    You should have received a copy of the GNU General Public License along
15a7c91847Schristos    with this program; if not, write to the Free Software Foundation,
16a7c91847Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*5a6c14c8Schristos #include <sys/cdefs.h>
18*5a6c14c8Schristos __RCSID("$NetBSD: setenv.c,v 1.2 2016/05/17 14:00:09 christos Exp $");
19*5a6c14c8Schristos 
20a7c91847Schristos 
21a7c91847Schristos #if HAVE_CONFIG_H
22a7c91847Schristos # include <config.h>
23a7c91847Schristos #endif
24a7c91847Schristos #include <alloca.h>
25a7c91847Schristos 
26a7c91847Schristos #include <errno.h>
27a7c91847Schristos #ifndef __set_errno
28a7c91847Schristos # define __set_errno(ev) ((errno) = (ev))
29a7c91847Schristos #endif
30a7c91847Schristos 
31a7c91847Schristos #include <stdlib.h>
32a7c91847Schristos #include <string.h>
33a7c91847Schristos #if _LIBC || HAVE_UNISTD_H
34a7c91847Schristos # include <unistd.h>
35a7c91847Schristos #endif
36a7c91847Schristos 
37a7c91847Schristos #if !_LIBC
38a7c91847Schristos # include "allocsa.h"
39a7c91847Schristos #endif
40a7c91847Schristos 
41a7c91847Schristos #if !_LIBC
42a7c91847Schristos # define __environ	environ
43a7c91847Schristos # ifndef HAVE_ENVIRON_DECL
44a7c91847Schristos extern char **environ;
45a7c91847Schristos # endif
46a7c91847Schristos #endif
47a7c91847Schristos 
48a7c91847Schristos #if _LIBC
49a7c91847Schristos /* This lock protects against simultaneous modifications of `environ'.  */
50a7c91847Schristos # include <bits/libc-lock.h>
51a7c91847Schristos __libc_lock_define_initialized (static, envlock)
52a7c91847Schristos # define LOCK	__libc_lock_lock (envlock)
53a7c91847Schristos # define UNLOCK	__libc_lock_unlock (envlock)
54a7c91847Schristos #else
55a7c91847Schristos # define LOCK
56a7c91847Schristos # define UNLOCK
57a7c91847Schristos #endif
58a7c91847Schristos 
59a7c91847Schristos /* In the GNU C library we must keep the namespace clean.  */
60a7c91847Schristos #ifdef _LIBC
61a7c91847Schristos # define setenv __setenv
62a7c91847Schristos # define clearenv __clearenv
63a7c91847Schristos # define tfind __tfind
64a7c91847Schristos # define tsearch __tsearch
65a7c91847Schristos #endif
66a7c91847Schristos 
67a7c91847Schristos /* In the GNU C library implementation we try to be more clever and
68a7c91847Schristos    allow arbitrarily many changes of the environment given that the used
69a7c91847Schristos    values are from a small set.  Outside glibc this will eat up all
70a7c91847Schristos    memory after a while.  */
71a7c91847Schristos #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \
72a7c91847Schristos 		      && defined __GNUC__)
73a7c91847Schristos # define USE_TSEARCH	1
74a7c91847Schristos # include <search.h>
75a7c91847Schristos typedef int (*compar_fn_t) (const void *, const void *);
76a7c91847Schristos 
77a7c91847Schristos /* This is a pointer to the root of the search tree with the known
78a7c91847Schristos    values.  */
79a7c91847Schristos static void *known_values;
80a7c91847Schristos 
81a7c91847Schristos # define KNOWN_VALUE(Str) \
82a7c91847Schristos   ({									      \
83a7c91847Schristos     void *value = tfind (Str, &known_values, (compar_fn_t) strcmp);	      \
84a7c91847Schristos     value != NULL ? *(char **) value : NULL;				      \
85a7c91847Schristos   })
86a7c91847Schristos # define STORE_VALUE(Str) \
87a7c91847Schristos   tsearch (Str, &known_values, (compar_fn_t) strcmp)
88a7c91847Schristos 
89a7c91847Schristos #else
90a7c91847Schristos # undef USE_TSEARCH
91a7c91847Schristos 
92a7c91847Schristos # define KNOWN_VALUE(Str) NULL
93a7c91847Schristos # define STORE_VALUE(Str) do { } while (0)
94a7c91847Schristos 
95a7c91847Schristos #endif
96a7c91847Schristos 
97a7c91847Schristos 
98a7c91847Schristos /* If this variable is not a null pointer we allocated the current
99a7c91847Schristos    environment.  */
100a7c91847Schristos static char **last_environ;
101a7c91847Schristos 
102a7c91847Schristos 
103a7c91847Schristos /* This function is used by `setenv' and `putenv'.  The difference between
104a7c91847Schristos    the two functions is that for the former must create a new string which
105a7c91847Schristos    is then placed in the environment, while the argument of `putenv'
106a7c91847Schristos    must be used directly.  This is all complicated by the fact that we try
107a7c91847Schristos    to reuse values once generated for a `setenv' call since we can never
108a7c91847Schristos    free the strings.  */
109a7c91847Schristos int
__add_to_environ(const char * name,const char * value,const char * combined,int replace)110a7c91847Schristos __add_to_environ (const char *name, const char *value, const char *combined,
111a7c91847Schristos 		  int replace)
112a7c91847Schristos {
113a7c91847Schristos   register char **ep;
114a7c91847Schristos   register size_t size;
115a7c91847Schristos   const size_t namelen = strlen (name);
116a7c91847Schristos   const size_t vallen = value != NULL ? strlen (value) + 1 : 0;
117a7c91847Schristos 
118a7c91847Schristos   LOCK;
119a7c91847Schristos 
120a7c91847Schristos   /* We have to get the pointer now that we have the lock and not earlier
121a7c91847Schristos      since another thread might have created a new environment.  */
122a7c91847Schristos   ep = __environ;
123a7c91847Schristos 
124a7c91847Schristos   size = 0;
125a7c91847Schristos   if (ep != NULL)
126a7c91847Schristos     {
127a7c91847Schristos       for (; *ep != NULL; ++ep)
128a7c91847Schristos 	if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
129a7c91847Schristos 	  break;
130a7c91847Schristos 	else
131a7c91847Schristos 	  ++size;
132a7c91847Schristos     }
133a7c91847Schristos 
134a7c91847Schristos   if (ep == NULL || *ep == NULL)
135a7c91847Schristos     {
136a7c91847Schristos       char **new_environ;
137a7c91847Schristos #ifdef USE_TSEARCH
138a7c91847Schristos       char *new_value;
139a7c91847Schristos #endif
140a7c91847Schristos 
141a7c91847Schristos       /* We allocated this space; we can extend it.  */
142a7c91847Schristos       new_environ =
143a7c91847Schristos 	(char **) (last_environ == NULL
144a7c91847Schristos 		   ? malloc ((size + 2) * sizeof (char *))
145a7c91847Schristos 		   : realloc (last_environ, (size + 2) * sizeof (char *)));
146a7c91847Schristos       if (new_environ == NULL)
147a7c91847Schristos 	{
148a7c91847Schristos 	  UNLOCK;
149a7c91847Schristos 	  return -1;
150a7c91847Schristos 	}
151a7c91847Schristos 
152a7c91847Schristos       /* If the whole entry is given add it.  */
153a7c91847Schristos       if (combined != NULL)
154a7c91847Schristos 	/* We must not add the string to the search tree since it belongs
155a7c91847Schristos 	   to the user.  */
156a7c91847Schristos 	new_environ[size] = (char *) combined;
157a7c91847Schristos       else
158a7c91847Schristos 	{
159a7c91847Schristos 	  /* See whether the value is already known.  */
160a7c91847Schristos #ifdef USE_TSEARCH
161a7c91847Schristos # ifdef _LIBC
162a7c91847Schristos 	  new_value = (char *) alloca (namelen + 1 + vallen);
163a7c91847Schristos 	  __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
164a7c91847Schristos 		     value, vallen);
165a7c91847Schristos # else
166a7c91847Schristos 	  new_value = (char *) allocsa (namelen + 1 + vallen);
167a7c91847Schristos 	  if (new_value == NULL)
168a7c91847Schristos 	    {
169a7c91847Schristos 	      __set_errno (ENOMEM);
170a7c91847Schristos 	      UNLOCK;
171a7c91847Schristos 	      return -1;
172a7c91847Schristos 	    }
173a7c91847Schristos 	  memcpy (new_value, name, namelen);
174a7c91847Schristos 	  new_value[namelen] = '=';
175a7c91847Schristos 	  memcpy (&new_value[namelen + 1], value, vallen);
176a7c91847Schristos # endif
177a7c91847Schristos 
178a7c91847Schristos 	  new_environ[size] = KNOWN_VALUE (new_value);
179a7c91847Schristos 	  if (new_environ[size] == NULL)
180a7c91847Schristos #endif
181a7c91847Schristos 	    {
182a7c91847Schristos 	      new_environ[size] = (char *) malloc (namelen + 1 + vallen);
183a7c91847Schristos 	      if (new_environ[size] == NULL)
184a7c91847Schristos 		{
185a7c91847Schristos #if defined USE_TSEARCH && !defined _LIBC
186a7c91847Schristos 		  freesa (new_value);
187a7c91847Schristos #endif
188a7c91847Schristos 		  __set_errno (ENOMEM);
189a7c91847Schristos 		  UNLOCK;
190a7c91847Schristos 		  return -1;
191a7c91847Schristos 		}
192a7c91847Schristos 
193a7c91847Schristos #ifdef USE_TSEARCH
194a7c91847Schristos 	      memcpy (new_environ[size], new_value, namelen + 1 + vallen);
195a7c91847Schristos #else
196a7c91847Schristos 	      memcpy (new_environ[size], name, namelen);
197a7c91847Schristos 	      new_environ[size][namelen] = '=';
198a7c91847Schristos 	      memcpy (&new_environ[size][namelen + 1], value, vallen);
199a7c91847Schristos #endif
200a7c91847Schristos 	      /* And save the value now.  We cannot do this when we remove
201a7c91847Schristos 		 the string since then we cannot decide whether it is a
202a7c91847Schristos 		 user string or not.  */
203a7c91847Schristos 	      STORE_VALUE (new_environ[size]);
204a7c91847Schristos 	    }
205a7c91847Schristos #if defined USE_TSEARCH && !defined _LIBC
206a7c91847Schristos 	  freesa (new_value);
207a7c91847Schristos #endif
208a7c91847Schristos 	}
209a7c91847Schristos 
210a7c91847Schristos       if (__environ != last_environ)
211a7c91847Schristos 	memcpy ((char *) new_environ, (char *) __environ,
212a7c91847Schristos 		size * sizeof (char *));
213a7c91847Schristos 
214a7c91847Schristos       new_environ[size + 1] = NULL;
215a7c91847Schristos 
216a7c91847Schristos       last_environ = __environ = new_environ;
217a7c91847Schristos     }
218a7c91847Schristos   else if (replace)
219a7c91847Schristos     {
220a7c91847Schristos       char *np;
221a7c91847Schristos 
222a7c91847Schristos       /* Use the user string if given.  */
223a7c91847Schristos       if (combined != NULL)
224a7c91847Schristos 	np = (char *) combined;
225a7c91847Schristos       else
226a7c91847Schristos 	{
227a7c91847Schristos #ifdef USE_TSEARCH
228a7c91847Schristos 	  char *new_value;
229a7c91847Schristos # ifdef _LIBC
230a7c91847Schristos 	  new_value = alloca (namelen + 1 + vallen);
231a7c91847Schristos 	  __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1),
232a7c91847Schristos 		     value, vallen);
233a7c91847Schristos # else
234a7c91847Schristos 	  new_value = allocsa (namelen + 1 + vallen);
235a7c91847Schristos 	  if (new_value == NULL)
236a7c91847Schristos 	    {
237a7c91847Schristos 	      __set_errno (ENOMEM);
238a7c91847Schristos 	      UNLOCK;
239a7c91847Schristos 	      return -1;
240a7c91847Schristos 	    }
241a7c91847Schristos 	  memcpy (new_value, name, namelen);
242a7c91847Schristos 	  new_value[namelen] = '=';
243a7c91847Schristos 	  memcpy (&new_value[namelen + 1], value, vallen);
244a7c91847Schristos # endif
245a7c91847Schristos 
246a7c91847Schristos 	  np = KNOWN_VALUE (new_value);
247a7c91847Schristos 	  if (np == NULL)
248a7c91847Schristos #endif
249a7c91847Schristos 	    {
250a7c91847Schristos 	      np = malloc (namelen + 1 + vallen);
251a7c91847Schristos 	      if (np == NULL)
252a7c91847Schristos 		{
253a7c91847Schristos #if defined USE_TSEARCH && !defined _LIBC
254a7c91847Schristos 		  freesa (new_value);
255a7c91847Schristos #endif
256a7c91847Schristos 		  __set_errno (ENOMEM);
257a7c91847Schristos 		  UNLOCK;
258a7c91847Schristos 		  return -1;
259a7c91847Schristos 		}
260a7c91847Schristos 
261a7c91847Schristos #ifdef USE_TSEARCH
262a7c91847Schristos 	      memcpy (np, new_value, namelen + 1 + vallen);
263a7c91847Schristos #else
264a7c91847Schristos 	      memcpy (np, name, namelen);
265a7c91847Schristos 	      np[namelen] = '=';
266a7c91847Schristos 	      memcpy (&np[namelen + 1], value, vallen);
267a7c91847Schristos #endif
268a7c91847Schristos 	      /* And remember the value.  */
269a7c91847Schristos 	      STORE_VALUE (np);
270a7c91847Schristos 	    }
271a7c91847Schristos #if defined USE_TSEARCH && !defined _LIBC
272a7c91847Schristos 	  freesa (new_value);
273a7c91847Schristos #endif
274a7c91847Schristos 	}
275a7c91847Schristos 
276a7c91847Schristos       *ep = np;
277a7c91847Schristos     }
278a7c91847Schristos 
279a7c91847Schristos   UNLOCK;
280a7c91847Schristos 
281a7c91847Schristos   return 0;
282a7c91847Schristos }
283a7c91847Schristos 
284a7c91847Schristos int
setenv(const char * name,const char * value,int replace)285a7c91847Schristos setenv (const char *name, const char *value, int replace)
286a7c91847Schristos {
287a7c91847Schristos   return __add_to_environ (name, value, NULL, replace);
288a7c91847Schristos }
289a7c91847Schristos 
290a7c91847Schristos /* The `clearenv' was planned to be added to POSIX.1 but probably
291a7c91847Schristos    never made it.  Nevertheless the POSIX.9 standard (POSIX bindings
292a7c91847Schristos    for Fortran 77) requires this function.  */
293a7c91847Schristos int
clearenv(void)294a7c91847Schristos clearenv (void)
295a7c91847Schristos {
296a7c91847Schristos   LOCK;
297a7c91847Schristos 
298a7c91847Schristos   if (__environ == last_environ && __environ != NULL)
299a7c91847Schristos     {
300a7c91847Schristos       /* We allocated this environment so we can free it.  */
301a7c91847Schristos       free (__environ);
302a7c91847Schristos       last_environ = NULL;
303a7c91847Schristos     }
304a7c91847Schristos 
305a7c91847Schristos   /* Clear the environment pointer removes the whole environment.  */
306a7c91847Schristos   __environ = NULL;
307a7c91847Schristos 
308a7c91847Schristos   UNLOCK;
309a7c91847Schristos 
310a7c91847Schristos   return 0;
311a7c91847Schristos }
312a7c91847Schristos 
313a7c91847Schristos #ifdef _LIBC
314a7c91847Schristos static void
free_mem(void)315a7c91847Schristos free_mem (void)
316a7c91847Schristos {
317a7c91847Schristos   /* Remove all traces.  */
318a7c91847Schristos   clearenv ();
319a7c91847Schristos 
320a7c91847Schristos   /* Now remove the search tree.  */
321a7c91847Schristos   __tdestroy (known_values, free);
322a7c91847Schristos   known_values = NULL;
323a7c91847Schristos }
324a7c91847Schristos text_set_element (__libc_subfreeres, free_mem);
325a7c91847Schristos 
326a7c91847Schristos 
327a7c91847Schristos # undef setenv
328a7c91847Schristos # undef clearenv
329a7c91847Schristos weak_alias (__setenv, setenv)
330a7c91847Schristos weak_alias (__clearenv, clearenv)
331a7c91847Schristos #endif
332