Merge from coreutils.
[gnulib.git] / lib / putenv.c
1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000, 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #if 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 #if !_LIBC
30 # if !defined errno
31 extern int errno;
32 # endif
33 # define __set_errno(ev) ((errno) = (ev))
34 #endif
35
36 /* Don't include stdlib.h because some (e.g., Solaris 2.7) declare putenv
37    with a non-const argument.  That would conflict with the declaration of
38    rpl_putenv below (due to the #define putenv rpl_putenv from config.h).  */
39
40 char *malloc ();
41 void free ();
42
43 #if defined (__GNU_LIBRARY__) || defined (HAVE_STRING_H)
44 # include <string.h>
45 #endif
46 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
47 # include <unistd.h>
48 #endif
49
50 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_STRCHR)
51 # define strchr index
52 #endif
53 #if !defined (__GNU_LIBRARY__) && !defined (HAVE_MEMCPY)
54 # define memcpy(d,s,n) bcopy ((s), (d), (n))
55 #endif
56
57 #if HAVE_GNU_LD
58 # define environ __environ
59 #else
60 extern char **environ;
61 #endif
62
63 #if _LIBC
64 /* This lock protects against simultaneous modifications of `environ'.  */
65 # include <bits/libc-lock.h>
66 __libc_lock_define_initialized (static, envlock)
67 # define LOCK   __libc_lock_lock (envlock)
68 # define UNLOCK __libc_lock_unlock (envlock)
69 #else
70 # define LOCK
71 # define UNLOCK
72 #endif
73
74 #ifndef NULL
75 # define NULL 0
76 #endif
77
78 static int
79 unsetenv (const char *name)
80 {
81   size_t len;
82   char **ep;
83
84   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
85     {
86       __set_errno (EINVAL);
87       return -1;
88     }
89
90   len = strlen (name);
91
92   LOCK;
93
94   ep = environ;
95   while (*ep != NULL)
96     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
97       {
98         /* Found it.  Remove this pointer by moving later ones back.  */
99         char **dp = ep;
100
101         do
102           dp[0] = dp[1];
103         while (*dp++);
104         /* Continue the loop in case NAME appears again.  */
105       }
106     else
107       ++ep;
108
109   UNLOCK;
110
111   return 0;
112 }
113
114
115 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
116    If STRING contains no `=', then remove STRING from the environment.  */
117 int
118 rpl_putenv (const char *string)
119 {
120   const char *const name_end = strchr (string, '=');
121   register size_t size;
122   register char **ep;
123
124   if (name_end == NULL)
125     {
126       /* Remove the variable from the environment.  */
127       return unsetenv (string);
128     }
129
130   size = 0;
131   for (ep = environ; *ep != NULL; ++ep)
132     if (!strncmp (*ep, string, name_end - string) &&
133         (*ep)[name_end - string] == '=')
134       break;
135     else
136       ++size;
137
138   if (*ep == NULL)
139     {
140       static char **last_environ = NULL;
141       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
142       if (new_environ == NULL)
143         return -1;
144       (void) memcpy ((void *) new_environ, (void *) environ,
145                      size * sizeof (char *));
146       new_environ[size] = (char *) string;
147       new_environ[size + 1] = NULL;
148       if (last_environ != NULL)
149         free (last_environ);
150       last_environ = new_environ;
151       environ = new_environ;
152     }
153   else
154     *ep = (char *) string;
155
156   return 0;
157 }