Use a consistent style for including <config.h>.
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000, 2003, 2004 Free Software Foundation, Inc.
2
3    NOTE: The canonical source of this file is maintained with the GNU C
4    Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
5
6    This program is free software; you can redistribute it and/or modify it
7    under the terms of the GNU General Public License as published by the
8    Free Software Foundation; either version 2, or (at your option) any
9    later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stddef.h>
25
26 /* Include errno.h *after* sys/types.h to work around header problems
27    on AIX 3.2.5.  */
28 #include <errno.h>
29 #ifndef __set_errno
30 # define __set_errno(ev) ((errno) = (ev))
31 #endif
32
33 /* Don't include stdlib.h because some (e.g., Solaris 7) declare putenv
34    with a non-const argument.  That would conflict with the declaration of
35    rpl_putenv below (due to the #define putenv rpl_putenv from config.h).  */
36
37 void *malloc ();
38 void free ();
39
40 #include <string.h>
41
42 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
43 # include <unistd.h>
44 #endif
45
46 #if HAVE_GNU_LD
47 # define environ __environ
48 #else
49 extern char **environ;
50 #endif
51
52 #if _LIBC
53 /* This lock protects against simultaneous modifications of `environ'.  */
54 # include <bits/libc-lock.h>
55 __libc_lock_define_initialized (static, envlock)
56 # define LOCK   __libc_lock_lock (envlock)
57 # define UNLOCK __libc_lock_unlock (envlock)
58 #else
59 # define LOCK
60 # define UNLOCK
61 #endif
62
63 static int
64 unsetenv (const char *name)
65 {
66   size_t len;
67   char **ep;
68
69   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
70     {
71       __set_errno (EINVAL);
72       return -1;
73     }
74
75   len = strlen (name);
76
77   LOCK;
78
79   ep = environ;
80   while (*ep != NULL)
81     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
82       {
83         /* Found it.  Remove this pointer by moving later ones back.  */
84         char **dp = ep;
85
86         do
87           dp[0] = dp[1];
88         while (*dp++);
89         /* Continue the loop in case NAME appears again.  */
90       }
91     else
92       ++ep;
93
94   UNLOCK;
95
96   return 0;
97 }
98
99
100 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
101    If STRING contains no `=', then remove STRING from the environment.  */
102 int
103 rpl_putenv (const char *string)
104 {
105   const char *const name_end = strchr (string, '=');
106   register size_t size;
107   register char **ep;
108
109   if (name_end == NULL)
110     {
111       /* Remove the variable from the environment.  */
112       return unsetenv (string);
113     }
114
115   size = 0;
116   for (ep = environ; *ep != NULL; ++ep)
117     if (!strncmp (*ep, string, name_end - string) &&
118         (*ep)[name_end - string] == '=')
119       break;
120     else
121       ++size;
122
123   if (*ep == NULL)
124     {
125       static char **last_environ = NULL;
126       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
127       if (new_environ == NULL)
128         return -1;
129       (void) memcpy ((void *) new_environ, (void *) environ,
130                      size * sizeof (char *));
131       new_environ[size] = (char *) string;
132       new_environ[size + 1] = NULL;
133       if (last_environ != NULL)
134         free (last_environ);
135       last_environ = new_environ;
136       environ = new_environ;
137     }
138   else
139     *ep = (char *) string;
140
141   return 0;
142 }