update from main archive 960910
[glibc.git] / sysdeps / generic / setenv.c
bloba84a8ebf64127ee27d3744fa7fabffb7e0abcaff
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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #ifdef 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 int
51 setenv (name, value, replace)
52 const char *name;
53 const char *value;
54 int replace;
56 register char **ep;
57 register size_t size;
58 const size_t namelen = strlen (name);
59 const size_t vallen = strlen (value) + 1;
61 LOCK;
63 size = 0;
64 for (ep = __environ; *ep != NULL; ++ep)
65 if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
66 break;
67 else
68 ++size;
70 if (*ep == NULL)
72 static char **last_environ;
73 char **new_environ;
74 if (__environ == last_environ)
75 /* We allocated this space; we can extend it. */
76 new_environ = (char **) realloc (last_environ,
77 (size + 2) * sizeof (char *));
78 else
79 new_environ = (char **) malloc ((size + 2) * sizeof (char *));
81 if (new_environ == NULL)
83 UNLOCK;
84 return -1;
87 new_environ[size] = malloc (namelen + 1 + vallen);
88 if (new_environ[size] == NULL)
90 free ((char *) new_environ);
91 errno = ENOMEM;
92 UNLOCK;
93 return -1;
96 if (__environ != last_environ)
97 memcpy ((char *) new_environ, (char *) __environ,
98 size * sizeof (char *));
100 memcpy (new_environ[size], name, namelen);
101 new_environ[size][namelen] = '=';
102 memcpy (&new_environ[size][namelen + 1], value, vallen);
104 new_environ[size + 1] = NULL;
106 last_environ = __environ = new_environ;
108 else if (replace)
110 size_t len = strlen (*ep);
111 if (len + 1 < namelen + 1 + vallen)
113 /* The existing string is too short; malloc a new one. */
114 char *new = malloc (namelen + 1 + vallen);
115 if (new == NULL)
117 UNLOCK;
118 return -1;
120 *ep = new;
122 memcpy (*ep, name, namelen);
123 (*ep)[namelen] = '=';
124 memcpy (&(*ep)[namelen + 1], value, vallen);
127 UNLOCK;
129 return 0;
132 void
133 unsetenv (name)
134 const char *name;
136 const size_t len = strlen (name);
137 char **ep;
139 LOCK;
141 for (ep = __environ; *ep; ++ep)
142 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
144 /* Found it. Remove this pointer by moving later ones back. */
145 char **dp = ep;
147 dp[0] = dp[1];
148 while (*dp++);
149 /* Continue the loop in case NAME appears again. */
152 UNLOCK;