Backport the :end-of-capability fix
[emacs.git] / lib / unsetenv.c
blob80810e78538f27e07280f42cb0bf304357555639
1 /* Copyright (C) 1992, 1995-2002, 2005-2015 Free Software Foundation,
2 Inc.
3 This file is part of the GNU C Library.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
19 optimizes away the name == NULL test below. */
20 #define _GL_ARG_NONNULL(params)
22 #include <config.h>
24 /* Specification. */
25 #include <stdlib.h>
27 #include <errno.h>
28 #if !_LIBC
29 # define __set_errno(ev) ((errno) = (ev))
30 #endif
32 #include <string.h>
33 #include <unistd.h>
35 #if !_LIBC
36 # define __environ environ
37 #endif
39 #if _LIBC
40 /* This lock protects against simultaneous modifications of 'environ'. */
41 # include <bits/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 /* In the GNU C library we must keep the namespace clean. */
51 #ifdef _LIBC
52 # define unsetenv __unsetenv
53 #endif
55 #if _LIBC || !HAVE_UNSETENV
57 int
58 unsetenv (const char *name)
60 size_t len;
61 char **ep;
63 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
65 __set_errno (EINVAL);
66 return -1;
69 len = strlen (name);
71 LOCK;
73 ep = __environ;
74 while (*ep != NULL)
75 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
77 /* Found it. Remove this pointer by moving later ones back. */
78 char **dp = ep;
81 dp[0] = dp[1];
82 while (*dp++);
83 /* Continue the loop in case NAME appears again. */
85 else
86 ++ep;
88 UNLOCK;
90 return 0;
93 #ifdef _LIBC
94 # undef unsetenv
95 weak_alias (__unsetenv, unsetenv)
96 #endif
98 #else /* HAVE_UNSETENV */
100 # undef unsetenv
101 # if !HAVE_DECL_UNSETENV
102 # if VOID_UNSETENV
103 extern void unsetenv (const char *);
104 # else
105 extern int unsetenv (const char *);
106 # endif
107 # endif
109 /* Call the underlying unsetenv, in case there is hidden bookkeeping
110 that needs updating beyond just modifying environ. */
112 rpl_unsetenv (const char *name)
114 int result = 0;
115 if (!name || !*name || strchr (name, '='))
117 errno = EINVAL;
118 return -1;
120 while (getenv (name))
121 # if !VOID_UNSETENV
122 result =
123 # endif
124 unsetenv (name);
125 return result;
128 #endif /* HAVE_UNSETENV */