1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000, 2003, 2004, 2005 Free Software Foundation, Inc.
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.
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
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.
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. */
26 /* Include errno.h *after* sys/types.h to work around header problems
30 # define __set_errno(ev) ((errno) = (ev))
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). */
45 # define environ __environ
47 extern char **environ;
51 /* This lock protects against simultaneous modifications of `environ'. */
52 # include <bits/libc-lock.h>
53 __libc_lock_define_initialized (static, envlock)
54 # define LOCK __libc_lock_lock (envlock)
55 # define UNLOCK __libc_lock_unlock (envlock)
62 unsetenv (const char *name)
67 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
79 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
81 /* Found it. Remove this pointer by moving later ones back. */
87 /* Continue the loop in case NAME appears again. */
98 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
99 If STRING contains no `=', then remove STRING from the environment. */
101 rpl_putenv (const char *string)
103 const char *const name_end = strchr (string, '=');
104 register size_t size;
107 if (name_end == NULL)
109 /* Remove the variable from the environment. */
110 return unsetenv (string);
114 for (ep = environ; *ep != NULL; ++ep)
115 if (!strncmp (*ep, string, name_end - string) &&
116 (*ep)[name_end - string] == '=')
123 static char **last_environ = NULL;
124 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
125 if (new_environ == NULL)
127 (void) memcpy ((void *) new_environ, (void *) environ,
128 size * sizeof (char *));
129 new_environ[size] = (char *) string;
130 new_environ[size + 1] = NULL;
131 if (last_environ != NULL)
133 last_environ = new_environ;
134 environ = new_environ;
137 *ep = (char *) string;