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