VirtualBox

source: kBuild/vendor/grep/current/gnulib-tests/putenv.c

Last change on this file was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1/* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2021 Free Software
2 Foundation, Inc.
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 file is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 3 of the
10 License, or (at your option) any later version.
11
12 This file 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 <https://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
37#if defined _WIN32 && ! defined __CYGWIN__
38# define WIN32_LEAN_AND_MEAN
39# include <windows.h>
40#endif
41
42#if _LIBC
43# if HAVE_GNU_LD
44# define environ __environ
45# else
46extern char **environ;
47# endif
48#endif
49
50#if _LIBC
51/* This lock protects against simultaneous modifications of 'environ'. */
52# include <bits/libc-lock.h>
53__libc_lock_define_initialized (static, envlock)
54# define LOCK __libc_lock_lock (envlock)
55# define UNLOCK __libc_lock_unlock (envlock)
56#else
57# define LOCK
58# define UNLOCK
59#endif
60
61#if defined _WIN32 && ! defined __CYGWIN__
62/* Don't assume that UNICODE is not defined. */
63# undef SetEnvironmentVariable
64# define SetEnvironmentVariable SetEnvironmentVariableA
65#endif
66
67static int
68_unsetenv (const char *name)
69{
70 size_t len;
71#if !HAVE_DECL__PUTENV
72 char **ep;
73#endif
74
75 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
76 {
77 __set_errno (EINVAL);
78 return -1;
79 }
80
81 len = strlen (name);
82
83#if HAVE_DECL__PUTENV
84 {
85 int putenv_result;
86 char *name_ = malloc (len + 2);
87 memcpy (name_, name, len);
88 name_[len] = '=';
89 name_[len + 1] = 0;
90 putenv_result = _putenv (name_);
91 free (name_);
92 return putenv_result;
93 }
94#else
95
96 LOCK;
97
98 ep = environ;
99 while (*ep != NULL)
100 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
101 {
102 /* Found it. Remove this pointer by moving later ones back. */
103 char **dp = ep;
104
105 do
106 dp[0] = dp[1];
107 while (*dp++);
108 /* Continue the loop in case NAME appears again. */
109 }
110 else
111 ++ep;
112
113 UNLOCK;
114
115 return 0;
116#endif
117}
118
119
120/* Put STRING, which is of the form "NAME=VALUE", in the environment.
121 If STRING contains no '=', then remove STRING from the environment. */
122int
123putenv (char *string)
124{
125 const char *name_end = strchr (string, '=');
126 char **ep;
127
128 if (name_end == NULL)
129 {
130 /* Remove the variable from the environment. */
131 return _unsetenv (string);
132 }
133
134#if HAVE_DECL__PUTENV
135 /* Rely on _putenv to allocate the new environment. If other
136 parts of the application use _putenv, the !HAVE_DECL__PUTENV code
137 would fight over who owns the environ vector, causing a crash. */
138 if (name_end[1])
139 return _putenv (string);
140 else
141 {
142 /* _putenv ("NAME=") unsets NAME, so invoke _putenv ("NAME= ")
143 to allocate the environ vector and then replace the new
144 entry with "NAME=". */
145 int putenv_result;
146 char *name_x = malloc (name_end - string + sizeof "= ");
147 if (!name_x)
148 return -1;
149 memcpy (name_x, string, name_end - string + 1);
150 name_x[name_end - string + 1] = ' ';
151 name_x[name_end - string + 2] = 0;
152 putenv_result = _putenv (name_x);
153 for (ep = environ; *ep; ep++)
154 if (strcmp (*ep, name_x) == 0)
155 {
156 *ep = string;
157 break;
158 }
159# if defined _WIN32 && ! defined __CYGWIN__
160 if (putenv_result == 0)
161 {
162 /* _putenv propagated "NAME= " into the subprocess environment;
163 fix that by calling SetEnvironmentVariable directly. */
164 name_x[name_end - string] = 0;
165 putenv_result = SetEnvironmentVariable (name_x, "") ? 0 : -1;
166 errno = ENOMEM; /* ENOMEM is the only way to fail. */
167 }
168# endif
169 free (name_x);
170 return putenv_result;
171 }
172#else
173 for (ep = environ; *ep; ep++)
174 if (strncmp (*ep, string, name_end - string) == 0
175 && (*ep)[name_end - string] == '=')
176 break;
177
178 if (*ep)
179 *ep = string;
180 else
181 {
182 static char **last_environ = NULL;
183 size_t size = ep - environ;
184 char **new_environ = malloc ((size + 2) * sizeof *new_environ);
185 if (! new_environ)
186 return -1;
187 new_environ[0] = string;
188 memcpy (new_environ + 1, environ, (size + 1) * sizeof *new_environ);
189 free (last_environ);
190 last_environ = new_environ;
191 environ = new_environ;
192 }
193
194 return 0;
195#endif
196}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette