1 /* Copyright (C) 1992, 1995, 1996, 1997, 2002, 2011 Free Software Foundation,
2    Inc.
3    This file based on setenv.c in the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18    Boston, MA 02110-1301, USA.  */
19 
20 
21 /*
22 
23 @deftypefn Supplemental int setenv (const char *@var{name}, @
24   const char *@var{value}, int @var{overwrite})
25 @deftypefnx Supplemental void unsetenv (const char *@var{name})
26 
27 @code{setenv} adds @var{name} to the environment with value
28 @var{value}.  If the name was already present in the environment,
29 the new value will be stored only if @var{overwrite} is nonzero.
30 The companion @code{unsetenv} function removes @var{name} from the
31 environment.  This implementation is not safe for multithreaded code.
32 
33 @end deftypefn
34 
35 */
36 
37 #if HAVE_CONFIG_H
38 # include <config.h>
39 #endif
40 
41 #define setenv libiberty_setenv
42 #define unsetenv libiberty_unsetenv
43 
44 #include "ansidecl.h"
45 #include <sys/types.h> /* For `size_t' */
46 #include <stdio.h>     /* For `NULL' */
47 
48 #include <errno.h>
49 #if !defined(errno) && !defined(HAVE_ERRNO_DECL)
50 extern int errno;
51 #endif
52 #define __set_errno(ev) ((errno) = (ev))
53 
54 #if HAVE_STDLIB_H
55 # include <stdlib.h>
56 #endif
57 #if HAVE_STRING_H
58 # include <string.h>
59 #endif
60 #if HAVE_UNISTD_H
61 # include <unistd.h>
62 #endif
63 
64 #define __environ	environ
65 #ifndef HAVE_ENVIRON_DECL
66 extern char **environ;
67 #endif
68 
69 #undef setenv
70 #undef unsetenv
71 
72 /* LOCK and UNLOCK are defined as no-ops.  This makes the libiberty
73  * implementation MT-Unsafe. */
74 #define LOCK
75 #define UNLOCK
76 
77 /* Below this point, it's verbatim code from the glibc-2.0 implementation */
78 
79 /* If this variable is not a null pointer we allocated the current
80    environment.  */
81 static char **last_environ;
82 
83 
84 int
setenv(const char * name,const char * value,int replace)85 setenv (const char *name, const char *value, int replace)
86 {
87   register char **ep = 0;
88   register size_t size;
89   const size_t namelen = strlen (name);
90   const size_t vallen = strlen (value) + 1;
91 
92   LOCK;
93 
94   size = 0;
95   if (__environ != NULL)
96     {
97       for (ep = __environ; *ep != NULL; ++ep)
98 	if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
99 	  break;
100 	else
101 	  ++size;
102     }
103 
104   if (__environ == NULL || *ep == NULL)
105     {
106       char **new_environ;
107       if (__environ == last_environ && __environ != NULL)
108 	/* We allocated this space; we can extend it.  */
109 	new_environ = (char **) realloc (last_environ,
110 					 (size + 2) * sizeof (char *));
111       else
112 	new_environ = (char **) malloc ((size + 2) * sizeof (char *));
113 
114       if (new_environ == NULL)
115 	{
116 	  UNLOCK;
117 	  return -1;
118 	}
119 
120       new_environ[size] = (char *) malloc (namelen + 1 + vallen);
121       if (new_environ[size] == NULL)
122 	{
123 	  free ((char *) new_environ);
124 	  __set_errno (ENOMEM);
125 	  UNLOCK;
126 	  return -1;
127 	}
128 
129       if (__environ != last_environ)
130 	memcpy ((char *) new_environ, (char *) __environ,
131 		size * sizeof (char *));
132 
133       memcpy (new_environ[size], name, namelen);
134       new_environ[size][namelen] = '=';
135       memcpy (&new_environ[size][namelen + 1], value, vallen);
136 
137       new_environ[size + 1] = NULL;
138 
139       last_environ = __environ = new_environ;
140     }
141   else if (replace)
142     {
143       size_t len = strlen (*ep);
144       if (len + 1 < namelen + 1 + vallen)
145 	{
146 	  /* The existing string is too short; malloc a new one.  */
147 	  char *new_string = (char *) malloc (namelen + 1 + vallen);
148 	  if (new_string == NULL)
149 	    {
150 	      UNLOCK;
151 	      return -1;
152 	    }
153 	  *ep = new_string;
154 	}
155       memcpy (*ep, name, namelen);
156       (*ep)[namelen] = '=';
157       memcpy (&(*ep)[namelen + 1], value, vallen);
158     }
159 
160   UNLOCK;
161 
162   return 0;
163 }
164 
165 void
unsetenv(const char * name)166 unsetenv (const char *name)
167 {
168   const size_t len = strlen (name);
169   char **ep;
170 
171   LOCK;
172 
173   for (ep = __environ; *ep; ++ep)
174     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
175       {
176 	/* Found it.  Remove this pointer by moving later ones back.  */
177 	char **dp = ep;
178 	do
179 	  dp[0] = dp[1];
180 	while (*dp++);
181 	/* Continue the loop in case NAME appears again.  */
182       }
183 
184   UNLOCK;
185 }
186