0ec392f866e9d353da7f649eba7fa09f00ce7503
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2013 Free Software
2    Foundation, Inc.
3
4    NOTE: The canonical source of this file is maintained with the GNU C
5    Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6
7    This program is free software: you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 3 of the License, or any
10    later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include <config.h>
21
22 /* Specification.  */
23 #include <stdlib.h>
24
25 #include <stddef.h>
26
27 /* Include errno.h *after* sys/types.h to work around header problems
28    on AIX 3.2.5.  */
29 #include <errno.h>
30 #ifndef __set_errno
31 # define __set_errno(ev) ((errno) = (ev))
32 #endif
33
34 #include <string.h>
35 #include <unistd.h>
36
37 #if _LIBC
38 # if HAVE_GNU_LD
39 #  define environ __environ
40 # else
41 extern char **environ;
42 # endif
43 #endif
44
45 #if _LIBC
46 /* This lock protects against simultaneous modifications of 'environ'.  */
47 # include <bits/libc-lock.h>
48 __libc_lock_define_initialized (static, envlock)
49 # define LOCK   __libc_lock_lock (envlock)
50 # define UNLOCK __libc_lock_unlock (envlock)
51 #else
52 # define LOCK
53 # define UNLOCK
54 #endif
55
56 static int
57 _unsetenv (const char *name)
58 {
59   size_t len;
60   char **ep;
61
62   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
63     {
64       __set_errno (EINVAL);
65       return -1;
66     }
67
68   len = strlen (name);
69
70   LOCK;
71
72   ep = environ;
73   while (*ep != NULL)
74     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
75       {
76         /* Found it.  Remove this pointer by moving later ones back.  */
77         char **dp = ep;
78
79         do
80           dp[0] = dp[1];
81         while (*dp++);
82         /* Continue the loop in case NAME appears again.  */
83       }
84     else
85       ++ep;
86
87   UNLOCK;
88
89   return 0;
90 }
91
92
93 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
94    If STRING contains no '=', then remove STRING from the environment.  */
95 int
96 putenv (char *string)
97 {
98   const char *const name_end = strchr (string, '=');
99   register size_t size;
100   register char **ep;
101
102   if (name_end == NULL)
103     {
104       /* Remove the variable from the environment.  */
105       return _unsetenv (string);
106     }
107
108   size = 0;
109   for (ep = environ; *ep != NULL; ++ep)
110     if (!strncmp (*ep, string, name_end - string) &&
111         (*ep)[name_end - string] == '=')
112       break;
113     else
114       ++size;
115
116   if (*ep == NULL)
117     {
118 #if HAVE__PUTENV
119       /* Rely on _putenv to allocate the new environment.  If other
120          parts of the application use _putenv, the !HAVE__PUTENV code
121          would fight over who owns the environ vector, causing a crash.  */
122       if (name_end[1])
123         return _putenv (string);
124       else
125         {
126           /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME=x")
127              to allocate the environ vector and then replace the new
128              entry with "NAME=".  */
129           int putenv_result, putenv_errno;
130           char *name_x = malloc (name_end - string + sizeof "=x");
131           if (!name_x)
132             return -1;
133           memcpy (name_x, string, name_end - string + 1);
134           name_x[name_end - string + 1] = 'x';
135           name_x[name_end - string + 2] = 0;
136           putenv_result = _putenv (name_x);
137           putenv_errno = errno;
138           for (ep = environ; *ep; ep++)
139             if (*ep == name_x)
140               {
141                 *ep = string;
142                 break;
143               }
144           free (name_x);
145           __set_errno (putenv_errno);
146           return putenv_result;
147         }
148 #else
149       static char **last_environ = NULL;
150       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
151       if (new_environ == NULL)
152         return -1;
153       (void) memcpy ((void *) new_environ, (void *) environ,
154                      size * sizeof (char *));
155       new_environ[size] = (char *) string;
156       new_environ[size + 1] = NULL;
157       free (last_environ);
158       last_environ = new_environ;
159       environ = new_environ;
160 #endif
161     }
162   else
163     *ep = string;
164
165   return 0;
166 }