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