1 /* Copyright (C) 1992, 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
25 #if _LIBC || HAVE_STDLIB_H
28 #if _LIBC || HAVE_STRING_H
31 #if _LIBC || HAVE_UNISTD_H
36 # define __environ environ
40 /* This lock protects against simultaneous modifications of `environ'. */
41 # include <libc-lock.h>
42 __libc_lock_define_initialized (static, envlock)
43 # define LOCK __libc_lock_lock (envlock)
44 # define UNLOCK __libc_lock_unlock (envlock)
50 /* If this variable is not a null pointer we allocated the current
52 static char **last_environ;
56 setenv (name, value, replace)
63 const size_t namelen = strlen (name);
64 const size_t vallen = strlen (value) + 1;
69 if (__environ != NULL)
70 for (ep = __environ; *ep != NULL; ++ep)
71 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
76 if (__environ == NULL || *ep == NULL)
79 if (__environ == last_environ && __environ != NULL)
80 /* We allocated this space; we can extend it. */
81 new_environ = (char **) realloc (last_environ,
82 (size + 2) * sizeof (char *));
84 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
86 if (new_environ == NULL)
92 new_environ[size] = malloc (namelen + 1 + vallen);
93 if (new_environ[size] == NULL)
95 free ((char *) new_environ);
101 if (__environ != last_environ)
102 memcpy ((char *) new_environ, (char *) __environ,
103 size * sizeof (char *));
105 memcpy (new_environ[size], name, namelen);
106 new_environ[size][namelen] = '=';
107 memcpy (&new_environ[size][namelen + 1], value, vallen);
109 new_environ[size + 1] = NULL;
111 last_environ = __environ = new_environ;
115 size_t len = strlen (*ep);
116 if (len + 1 < namelen + 1 + vallen)
118 /* The existing string is too short; malloc a new one. */
119 char *new = malloc (namelen + 1 + vallen);
127 memcpy (*ep, name, namelen);
128 (*ep)[namelen] = '=';
129 memcpy (&(*ep)[namelen + 1], value, vallen);
141 const size_t len = strlen (name);
146 for (ep = __environ; *ep; ++ep)
147 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
149 /* Found it. Remove this pointer by moving later ones back. */
154 /* Continue the loop in case NAME appears again. */
160 /* The `clearenv' was planned to be added to POSIX.1 but probably
161 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
162 for Fortran 77) requires this function. */
168 if (__environ == last_environ && __environ != NULL)
170 /* We allocated this environment so we can free it. */
175 /* Clear the environment pointer removes the whole environment. */