1 /* Copyright (C) 1992, 1995, 1996, 1997, 2002, 2011 Free Software Foundation,
3 This file based on setenv.c in the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
23 @deftypefn Supplemental int setenv (const char *@var{name}, @
24 const char *@var{value}, int @var{overwrite})
25 @deftypefnx Supplemental void unsetenv (const char *@var{name})
27 @code{setenv} adds @var{name} to the environment with value
28 @var{value}. If the name was already present in the environment,
29 the new value will be stored only if @var{overwrite} is nonzero.
30 The companion @code{unsetenv} function removes @var{name} from the
31 environment. This implementation is not safe for multithreaded code.
41 #define setenv libiberty_setenv
42 #define unsetenv libiberty_unsetenv
45 #include <sys/types.h> /* For `size_t' */
46 #include <stdio.h> /* For `NULL' */
49 #if !defined(errno) && !defined(HAVE_ERRNO_DECL)
52 #define __set_errno(ev) ((errno) = (ev))
64 #define __environ environ
65 #ifndef HAVE_ENVIRON_DECL
67 extern char **environ;
74 /* LOCK and UNLOCK are defined as no-ops. This makes the libiberty
75 * implementation MT-Unsafe. */
79 /* Below this point, it's verbatim code from the glibc-2.0 implementation */
81 /* If this variable is not a null pointer we allocated the current
83 static char **last_environ;
87 setenv (const char *name, const char *value, int replace)
89 register char **ep = 0;
91 const size_t namelen = strlen (name);
92 const size_t vallen = strlen (value) + 1;
97 if (__environ != NULL)
99 for (ep = __environ; *ep != NULL; ++ep)
100 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
106 if (__environ == NULL || *ep == NULL)
109 if (__environ == last_environ && __environ != NULL)
110 /* We allocated this space; we can extend it. */
111 new_environ = (char **) realloc (last_environ,
112 (size + 2) * sizeof (char *));
114 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
116 if (new_environ == NULL)
122 new_environ[size] = (char *) malloc (namelen + 1 + vallen);
123 if (new_environ[size] == NULL)
125 free ((char *) new_environ);
126 __set_errno (ENOMEM);
131 if (__environ != last_environ)
132 memcpy ((char *) new_environ, (char *) __environ,
133 size * sizeof (char *));
135 memcpy (new_environ[size], name, namelen);
136 new_environ[size][namelen] = '=';
137 memcpy (&new_environ[size][namelen + 1], value, vallen);
139 new_environ[size + 1] = NULL;
141 last_environ = __environ = new_environ;
145 size_t len = strlen (*ep);
146 if (len + 1 < namelen + 1 + vallen)
148 /* The existing string is too short; malloc a new one. */
149 char *new_string = (char *) malloc (namelen + 1 + vallen);
150 if (new_string == NULL)
157 memcpy (*ep, name, namelen);
158 (*ep)[namelen] = '=';
159 memcpy (&(*ep)[namelen + 1], value, vallen);
168 unsetenv (const char *name)
170 const size_t len = strlen (name);
175 for (ep = __environ; *ep; ++ep)
176 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
178 /* Found it. Remove this pointer by moving later ones back. */
183 /* Continue the loop in case NAME appears again. */