1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000, 2003, 2004, 2005, 2006, 2007
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 3 of the License, or 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, see <http://www.gnu.org/licenses/>. */
27 /* Include errno.h *after* sys/types.h to work around header problems
31 # define __set_errno(ev) ((errno) = (ev))
39 # define environ __environ
41 extern char **environ;
45 /* This lock protects against simultaneous modifications of `environ'. */
46 # include <bits/libc-lock.h>
47 __libc_lock_define_initialized (static, envlock)
48 # define LOCK __libc_lock_lock (envlock)
49 # define UNLOCK __libc_lock_unlock (envlock)
56 _unsetenv (const char *name)
61 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
73 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
75 /* Found it. Remove this pointer by moving later ones back. */
81 /* Continue the loop in case NAME appears again. */
92 /* Put STRING, which is of the form "NAME=VALUE", in the environment.
93 If STRING contains no `=', then remove STRING from the environment. */
97 const char *const name_end = strchr (string, '=');
101 if (name_end == NULL)
103 /* Remove the variable from the environment. */
104 return _unsetenv (string);
108 for (ep = environ; *ep != NULL; ++ep)
109 if (!strncmp (*ep, string, name_end - string) &&
110 (*ep)[name_end - string] == '=')
117 static char **last_environ = NULL;
118 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
119 if (new_environ == NULL)
121 (void) memcpy ((void *) new_environ, (void *) environ,
122 size * sizeof (char *));
123 new_environ[size] = (char *) string;
124 new_environ[size + 1] = NULL;
125 if (last_environ != NULL)
127 last_environ = new_environ;
128 environ = new_environ;