]> git.cworth.org Git - tar/blob - gnu/unsetenv.c
upstream: Fix extraction of device nodes.
[tar] / gnu / unsetenv.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 1992, 1995-2002, 2005-2010 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 <http://www.gnu.org/licenses/>.  */
18
19 #include <config.h>
20
21 /* Don't use __attribute__ __nonnull__ in this compilation unit.  Otherwise gcc
22    optimizes away the name == NULL test below.  */
23 #define _GL_ARG_NONNULL(params)
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>
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
103 /* Call the underlying unsetenv, in case there is hidden bookkeeping
104    that needs updating beyond just modifying environ.  */
105 int
106 rpl_unsetenv (const char *name)
107 {
108   int result = 0;
109   if (!name || !*name || strchr (name, '='))
110     {
111       errno = EINVAL;
112       return -1;
113     }
114   while (getenv (name))
115 # if !VOID_UNSETENV
116     result =
117 # endif
118       unsetenv (name);
119   return result;
120 }
121
122 #endif /* HAVE_UNSETENV */