update from main archive 961101
[glibc/history.git] / sysdeps / generic / setenv.c
blob9403ccb03541e5f8d8cdc0f5008f8aeee32ad3f6
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. */
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <errno.h>
25 #if _LIBC || HAVE_STDLIB_H
26 # include <stdlib.h>
27 #endif
28 #if _LIBC || HAVE_STRING_H
29 # include <string.h>
30 #endif
31 #if _LIBC || HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
35 #if _LIBC - 0 == 0
36 # define __environ environ
37 #endif
39 #if _LIBC - 0
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)
45 #else
46 # define LOCK
47 # define UNLOCK
48 #endif
50 /* If this variable is not a null pointer we allocated the current
51 environment. */
52 static char **last_environ;
55 int
56 setenv (name, value, replace)
57 const char *name;
58 const char *value;
59 int replace;
61 register char **ep;
62 register size_t size;
63 const size_t namelen = strlen (name);
64 const size_t vallen = strlen (value) + 1;
66 LOCK;
68 size = 0;
69 if (__environ != NULL)
70 for (ep = __environ; *ep != NULL; ++ep)
71 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
72 break;
73 else
74 ++size;
76 if (__environ == NULL || *ep == NULL)
78 char **new_environ;
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 *));
83 else
84 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
86 if (new_environ == NULL)
88 UNLOCK;
89 return -1;
92 new_environ[size] = malloc (namelen + 1 + vallen);
93 if (new_environ[size] == NULL)
95 free ((char *) new_environ);
96 __set_errno (ENOMEM);
97 UNLOCK;
98 return -1;
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;
113 else if (replace)
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);
120 if (new == NULL)
122 UNLOCK;
123 return -1;
125 *ep = new;
127 memcpy (*ep, name, namelen);
128 (*ep)[namelen] = '=';
129 memcpy (&(*ep)[namelen + 1], value, vallen);
132 UNLOCK;
134 return 0;
137 void
138 unsetenv (name)
139 const char *name;
141 const size_t len = strlen (name);
142 char **ep;
144 LOCK;
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. */
150 char **dp = ep;
152 dp[0] = dp[1];
153 while (*dp++);
154 /* Continue the loop in case NAME appears again. */
157 UNLOCK;
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. */
164 clearenv ()
166 LOCK;
168 if (__environ == last_environ && __environ != NULL)
170 /* We allocated this environment so we can free it. */
171 free (__environ);
172 last_environ = NULL;
175 /* Clear the environment pointer removes the whole environment. */
176 __environ = NULL;
178 UNLOCK;
180 return 0;