Handle invalid suffixes and overflow independently, so that
[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 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 void *malloc ();
41 void free ();
42
43 #include <string.h>
44
45 #if defined (__GNU_LIBRARY__) || defined (HAVE_UNISTD_H)
46 # include <unistd.h>
47 #endif
48
49 #if HAVE_GNU_LD
50 # define environ __environ
51 #else
52 extern char **environ;
53 #endif
54
55 #if _LIBC
56 /* This lock protects against simultaneous modifications of `environ'.  */
57 # include <bits/libc-lock.h>
58 __libc_lock_define_initialized (static, envlock)
59 # define LOCK   __libc_lock_lock (envlock)
60 # define UNLOCK __libc_lock_unlock (envlock)
61 #else
62 # define LOCK
63 # define UNLOCK
64 #endif
65
66 static int
67 unsetenv (const char *name)
68 {
69   size_t len;
70   char **ep;
71
72   if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
73     {
74       __set_errno (EINVAL);
75       return -1;
76     }
77
78   len = strlen (name);
79
80   LOCK;
81
82   ep = environ;
83   while (*ep != NULL)
84     if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
85       {
86         /* Found it.  Remove this pointer by moving later ones back.  */
87         char **dp = ep;
88
89         do
90           dp[0] = dp[1];
91         while (*dp++);
92         /* Continue the loop in case NAME appears again.  */
93       }
94     else
95       ++ep;
96
97   UNLOCK;
98
99   return 0;
100 }
101
102
103 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
104    If STRING contains no `=', then remove STRING from the environment.  */
105 int
106 rpl_putenv (const char *string)
107 {
108   const char *const name_end = strchr (string, '=');
109   register size_t size;
110   register char **ep;
111
112   if (name_end == NULL)
113     {
114       /* Remove the variable from the environment.  */
115       return unsetenv (string);
116     }
117
118   size = 0;
119   for (ep = environ; *ep != NULL; ++ep)
120     if (!strncmp (*ep, string, name_end - string) &&
121         (*ep)[name_end - string] == '=')
122       break;
123     else
124       ++size;
125
126   if (*ep == NULL)
127     {
128       static char **last_environ = NULL;
129       char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
130       if (new_environ == NULL)
131         return -1;
132       (void) memcpy ((void *) new_environ, (void *) environ,
133                      size * sizeof (char *));
134       new_environ[size] = (char *) string;
135       new_environ[size + 1] = NULL;
136       if (last_environ != NULL)
137         free (last_environ);
138       last_environ = new_environ;
139       environ = new_environ;
140     }
141   else
142     *ep = (char *) string;
143
144   return 0;
145 }