1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000, 2003, 2004, 2005, 2006
2 Free Software Foundation, Inc.
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.
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
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.
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. */
23 /* undef putenv here, because some (e.g., Solaris 10) declare putenv in
24 with a non-const argument. That would conflict with the declaration of
25 rpl_putenv below (due to the #define putenv rpl_putenv from config.h). */
27 int rpl_putenv (char const *);
31 /* Include errno.h *after* sys/types.h to work around header problems
35 # define __set_errno(ev) ((errno) = (ev))
43 # define environ __environ
45 extern char **environ;
49 /* This lock protects against simultaneous modifications of `environ'. */
50 # include <bits/libc-lock.h>
51 __libc_lock_define_initialized (static, envlock)
52 # define LOCK __libc_lock_lock (envlock)
53 # define UNLOCK __libc_lock_unlock (envlock)
60 unsetenv (const char *name)
65 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
77 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
79 /* Found it. Remove this pointer by moving later ones back. */
85 /* Continue the loop in case NAME appears again. */
96 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
97 If STRING contains no `=', then remove STRING from the environment. */
99 rpl_putenv (const char *string)
101 const char *const name_end = strchr (string, '=');
102 register size_t size;
105 if (name_end == NULL)
107 /* Remove the variable from the environment. */
108 return unsetenv (string);
112 for (ep = environ; *ep != NULL; ++ep)
113 if (!strncmp (*ep, string, name_end - string) &&
114 (*ep)[name_end - string] == '=')
121 static char **last_environ = NULL;
122 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
123 if (new_environ == NULL)
125 (void) memcpy ((void *) new_environ, (void *) environ,
126 size * sizeof (char *));
127 new_environ[size] = (char *) string;
128 new_environ[size + 1] = NULL;
129 if (last_environ != NULL)
131 last_environ = new_environ;
132 environ = new_environ;
135 *ep = (char *) string;