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