1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 1992, 1995-2002, 2005-2019 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18
19 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
20 optimizes away the name == NULL test below. */
21 #define _GL_ARG_NONNULL(params)
22
23 #include <config.h>
24
25 /* Specification. */
26 #include <stdlib.h>
27
28 #include <errno.h>
29 #if !_LIBC
30 # define __set_errno(ev) ((errno) = (ev))
31 #endif
32
33 #include <string.h>
34 #include <unistd.h>
35
36 #if !_LIBC
37 # define __environ environ
38 #endif
39
40 #if _LIBC
41 /* This lock protects against simultaneous modifications of 'environ'. */
42 # include <bits/libc-lock.h>
__libc_lock_define_initialized(static,envlock)43 __libc_lock_define_initialized (static, envlock)
44 # define LOCK __libc_lock_lock (envlock)
45 # define UNLOCK __libc_lock_unlock (envlock)
46 #else
47 # define LOCK
48 # define UNLOCK
49 #endif
50
51 /* In the GNU C library we must keep the namespace clean. */
52 #ifdef _LIBC
53 # define unsetenv __unsetenv
54 #endif
55
56 #if _LIBC || !HAVE_UNSETENV
57
58 int
59 unsetenv (const char *name)
60 {
61 size_t len;
62 char **ep;
63
64 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
65 {
66 __set_errno (EINVAL);
67 return -1;
68 }
69
70 len = strlen (name);
71
72 LOCK;
73
74 ep = __environ;
75 while (*ep != NULL)
76 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
77 {
78 /* Found it. Remove this pointer by moving later ones back. */
79 char **dp = ep;
80
81 do
82 dp[0] = dp[1];
83 while (*dp++);
84 /* Continue the loop in case NAME appears again. */
85 }
86 else
87 ++ep;
88
89 UNLOCK;
90
91 return 0;
92 }
93
94 #ifdef _LIBC
95 # undef unsetenv
96 weak_alias (__unsetenv, unsetenv)
97 #endif
98
99 #else /* HAVE_UNSETENV */
100
101 # undef unsetenv
102 # if !HAVE_DECL_UNSETENV
103 # if VOID_UNSETENV
104 extern void unsetenv (const char *);
105 # else
106 extern int unsetenv (const char *);
107 # endif
108 # endif
109
110 /* Call the underlying unsetenv, in case there is hidden bookkeeping
111 that needs updating beyond just modifying environ. */
112 int
113 rpl_unsetenv (const char *name)
114 {
115 int result = 0;
116 if (!name || !*name || strchr (name, '='))
117 {
118 errno = EINVAL;
119 return -1;
120 }
121 while (getenv (name))
122 # if !VOID_UNSETENV
123 result =
124 # endif
125 unsetenv (name);
126 return result;
127 }
128
129 #endif /* HAVE_UNSETENV */
130