temporarily disable elisp exception tests
[bpt/guile.git] / lib / putenv.c
CommitLineData
5e69ceb7 1/* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2014 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
5e69ceb7
MW
37#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
38# define WIN32_LEAN_AND_MEAN
39# include <windows.h>
40#endif
41
35428fb6
LC
42#if _LIBC
43# if HAVE_GNU_LD
7f1ea859 44# define environ __environ
35428fb6 45# else
f240aacb 46extern char **environ;
35428fb6 47# endif
f240aacb
LC
48#endif
49
50#if _LIBC
f0007cad 51/* This lock protects against simultaneous modifications of 'environ'. */
f240aacb
LC
52# include <bits/libc-lock.h>
53__libc_lock_define_initialized (static, envlock)
1cd4fffc
LC
54# define LOCK __libc_lock_lock (envlock)
55# define UNLOCK __libc_lock_unlock (envlock)
f240aacb
LC
56#else
57# define LOCK
58# define UNLOCK
59#endif
60
61static int
62_unsetenv (const char *name)
63{
64 size_t len;
5e69ceb7 65#if !HAVE_DECL__PUTENV
f240aacb 66 char **ep;
5e69ceb7 67#endif
f240aacb
LC
68
69 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
70 {
71 __set_errno (EINVAL);
72 return -1;
73 }
74
75 len = strlen (name);
76
5e69ceb7
MW
77#if HAVE_DECL__PUTENV
78 {
79 int putenv_result, putenv_errno;
80 char *name_ = malloc (len + 2);
81 memcpy (name_, name, len);
82 name_[len] = '=';
83 name_[len + 1] = 0;
84 putenv_result = _putenv (name_);
85 putenv_errno = errno;
86 free (name_);
87 __set_errno (putenv_errno);
88 return putenv_result;
89 }
90#else
91
f240aacb
LC
92 LOCK;
93
94 ep = environ;
95 while (*ep != NULL)
96 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
97 {
1cd4fffc
LC
98 /* Found it. Remove this pointer by moving later ones back. */
99 char **dp = ep;
f240aacb 100
1cd4fffc
LC
101 do
102 dp[0] = dp[1];
103 while (*dp++);
104 /* Continue the loop in case NAME appears again. */
f240aacb
LC
105 }
106 else
107 ++ep;
108
109 UNLOCK;
110
111 return 0;
5e69ceb7 112#endif
f240aacb
LC
113}
114
115
116/* Put STRING, which is of the form "NAME=VALUE", in the environment.
f0007cad 117 If STRING contains no '=', then remove STRING from the environment. */
f240aacb
LC
118int
119putenv (char *string)
120{
5e69ceb7
MW
121 const char *name_end = strchr (string, '=');
122 char **ep;
f240aacb
LC
123
124 if (name_end == NULL)
125 {
126 /* Remove the variable from the environment. */
127 return _unsetenv (string);
128 }
129
5e69ceb7
MW
130#if HAVE_DECL__PUTENV
131 /* Rely on _putenv to allocate the new environment. If other
132 parts of the application use _putenv, the !HAVE_DECL__PUTENV code
133 would fight over who owns the environ vector, causing a crash. */
134 if (name_end[1])
135 return _putenv (string);
136 else
f240aacb 137 {
5e69ceb7
MW
138 /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
139 to allocate the environ vector and then replace the new
140 entry with "NAME=". */
141 int putenv_result, putenv_errno;
142 char *name_x = malloc (name_end - string + sizeof "= ");
143 if (!name_x)
144 return -1;
145 memcpy (name_x, string, name_end - string + 1);
146 name_x[name_end - string + 1] = ' ';
147 name_x[name_end - string + 2] = 0;
148 putenv_result = _putenv (name_x);
149 putenv_errno = errno;
150 for (ep = environ; *ep; ep++)
151 if (strcmp (*ep, name_x) == 0)
152 {
153 *ep = string;
154 break;
155 }
156# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
157 if (putenv_result == 0)
af07e104 158 {
5e69ceb7
MW
159 /* _putenv propagated "NAME= " into the subprocess environment;
160 fix that by calling SetEnvironmentVariable directly. */
161 name_x[name_end - string] = 0;
162 putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
163 putenv_errno = ENOMEM; /* ENOMEM is the only way to fail. */
af07e104 164 }
5e69ceb7
MW
165# endif
166 free (name_x);
167 __set_errno (putenv_errno);
168 return putenv_result;
169 }
af07e104 170#else
5e69ceb7
MW
171 for (ep = environ; *ep; ep++)
172 if (strncmp (*ep, string, name_end - string) == 0
173 && (*ep)[name_end - string] == '=')
174 break;
175
176 if (*ep)
177 *ep = string;
178 else
179 {
f240aacb 180 static char **last_environ = NULL;
5e69ceb7
MW
181 size_t size = ep - environ;
182 char **new_environ = malloc ((size + 2) * sizeof *new_environ);
183 if (! new_environ)
1cd4fffc 184 return -1;
5e69ceb7
MW
185 new_environ[0] = string;
186 memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
f240aacb
LC
187 free (last_environ);
188 last_environ = new_environ;
189 environ = new_environ;
190 }
f240aacb
LC
191
192 return 0;
5e69ceb7 193#endif
f240aacb 194}