Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000, 2003, 2004, 2005, 2006, 2007
2    Free Software 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 /* undef putenv here, because some (e.g., Solaris 10) declare putenv in
23    with a non-const argument.  That would conflict with the declaration of
24    rpl_putenv below (due to the #define putenv rpl_putenv from config.h).  */
25 #undef putenv
26 int rpl_putenv (char const *);
27
28 #include <stddef.h>
29
30 /* Include errno.h *after* sys/types.h to work around header problems
31    on AIX 3.2.5.  */
32 #include <errno.h>
33 #ifndef __set_errno
34 # define __set_errno(ev) ((errno) = (ev))
35 #endif
36
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #if HAVE_GNU_LD
42 # define environ __environ
43 #else
44 extern char **environ;
45 #endif
46
47 #if _LIBC
48 /* This lock protects against simultaneous modifications of `environ'.  */
49 # include <bits/libc-lock.h>
50 __libc_lock_define_initialized (static, envlock)
51 # define LOCK   __libc_lock_lock (envlock)
52 # define UNLOCK __libc_lock_unlock (envlock)
53 #else
54 # define LOCK
55 # define UNLOCK
56 #endif
57
58 static int
59 _unsetenv (const char *name)
60 {
61   size_t len;
62   char **ep;
63
64   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
65     {
66       __set_errno (EINVAL);
67       return -1;
68     }
69
70   len = strlen (name);
71
72   LOCK;
73
74   ep = environ;
75   while (*ep != NULL)
76     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
77       {
78         /* Found it.  Remove this pointer by moving later ones back.  */
79         char **dp = ep;
80
81         do
82           dp[0] = dp[1];
83         while (*dp++);
84         /* Continue the loop in case NAME appears again.  */
85       }
86     else
87       ++ep;
88
89   UNLOCK;
90
91   return 0;
92 }
93
94
95 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
96    If STRING contains no `=', then remove STRING from the environment.  */
97 int
98 rpl_putenv (const char *string)
99 {
100   const char *const name_end = strchr (string, '=');
101   register size_t size;
102   register char **ep;
103
104   if (name_end == NULL)
105     {
106       /* Remove the variable from the environment.  */
107       return _unsetenv (string);
108     }
109
110   size = 0;
111   for (ep = environ; *ep != NULL; ++ep)
112     if (!strncmp (*ep, string, name_end - string) &&
113         (*ep)[name_end - string] == '=')
114       break;
115     else
116       ++size;
117
118   if (*ep == NULL)
119     {
120       static char **last_environ = NULL;
121       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
122       if (new_environ == NULL)
123         return -1;
124       (void) memcpy ((void *) new_environ, (void *) environ,
125                      size * sizeof (char *));
126       new_environ[size] = (char *) string;
127       new_environ[size + 1] = NULL;
128       if (last_environ != NULL)
129         free (last_environ);
130       last_environ = new_environ;
131       environ = new_environ;
132     }
133   else
134     *ep = (char *) string;
135
136   return 0;
137 }