Update to gnulib 0.0.7865-a828.
[bpt/guile.git] / lib / putenv.c
CommitLineData
af07e104 1/* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2013 Free Software
61cd9dc9 2 Foundation, Inc.
f240aacb
LC
3
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.
6
7 This program is free software: you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or any
10 later version.
11
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 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include <config.h>
21
22/* Specification. */
23#include <stdlib.h>
24
25#include <stddef.h>
26
27/* Include errno.h *after* sys/types.h to work around header problems
28 on AIX 3.2.5. */
29#include <errno.h>
30#ifndef __set_errno
31# define __set_errno(ev) ((errno) = (ev))
32#endif
33
34#include <string.h>
35#include <unistd.h>
36
35428fb6
LC
37#if _LIBC
38# if HAVE_GNU_LD
7f1ea859 39# define environ __environ
35428fb6 40# else
f240aacb 41extern char **environ;
35428fb6 42# endif
f240aacb
LC
43#endif
44
45#if _LIBC
f0007cad 46/* This lock protects against simultaneous modifications of 'environ'. */
f240aacb
LC
47# include <bits/libc-lock.h>
48__libc_lock_define_initialized (static, envlock)
1cd4fffc
LC
49# define LOCK __libc_lock_lock (envlock)
50# define UNLOCK __libc_lock_unlock (envlock)
f240aacb
LC
51#else
52# define LOCK
53# define UNLOCK
54#endif
55
56static int
57_unsetenv (const char *name)
58{
59 size_t len;
60 char **ep;
61
62 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
63 {
64 __set_errno (EINVAL);
65 return -1;
66 }
67
68 len = strlen (name);
69
70 LOCK;
71
72 ep = environ;
73 while (*ep != NULL)
74 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
75 {
1cd4fffc
LC
76 /* Found it. Remove this pointer by moving later ones back. */
77 char **dp = ep;
f240aacb 78
1cd4fffc
LC
79 do
80 dp[0] = dp[1];
81 while (*dp++);
82 /* Continue the loop in case NAME appears again. */
f240aacb
LC
83 }
84 else
85 ++ep;
86
87 UNLOCK;
88
89 return 0;
90}
91
92
93/* Put STRING, which is of the form "NAME=VALUE", in the environment.
f0007cad 94 If STRING contains no '=', then remove STRING from the environment. */
f240aacb
LC
95int
96putenv (char *string)
97{
98 const char *const name_end = strchr (string, '=');
99 register size_t size;
100 register char **ep;
101
102 if (name_end == NULL)
103 {
104 /* Remove the variable from the environment. */
105 return _unsetenv (string);
106 }
107
108 size = 0;
109 for (ep = environ; *ep != NULL; ++ep)
110 if (!strncmp (*ep, string, name_end - string) &&
1cd4fffc 111 (*ep)[name_end - string] == '=')
f240aacb
LC
112 break;
113 else
114 ++size;
115
116 if (*ep == NULL)
117 {
af07e104
AW
118#if HAVE__PUTENV
119 /* Rely on _putenv to allocate the new environment. If other
120 parts of the application use _putenv, the !HAVE__PUTENV code
121 would fight over who owns the environ vector, causing a crash. */
122 if (name_end[1])
123 return _putenv (string);
124 else
125 {
126 /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME=x")
127 to allocate the environ vector and then replace the new
128 entry with "NAME=". */
129 int putenv_result, putenv_errno;
130 char *name_x = malloc (name_end - string + sizeof "=x");
131 if (!name_x)
132 return -1;
133 memcpy (name_x, string, name_end - string + 1);
134 name_x[name_end - string + 1] = 'x';
135 name_x[name_end - string + 2] = 0;
136 putenv_result = _putenv (name_x);
137 putenv_errno = errno;
138 for (ep = environ; *ep; ep++)
139 if (*ep == name_x)
140 {
141 *ep = string;
142 break;
143 }
144 free (name_x);
145 __set_errno (putenv_errno);
146 return putenv_result;
147 }
148#else
f240aacb
LC
149 static char **last_environ = NULL;
150 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
151 if (new_environ == NULL)
1cd4fffc 152 return -1;
f240aacb 153 (void) memcpy ((void *) new_environ, (void *) environ,
1cd4fffc 154 size * sizeof (char *));
f240aacb
LC
155 new_environ[size] = (char *) string;
156 new_environ[size + 1] = NULL;
157 free (last_environ);
158 last_environ = new_environ;
159 environ = new_environ;
af07e104 160#endif
f240aacb
LC
161 }
162 else
163 *ep = string;
164
165 return 0;
166}