annotate lib/unsetenv.c @ 17326:bb52d9cece01

unsetenv etc.: port to Solaris 11 + GNU Emacs * lib/canonicalize-lgpl.c, lib/getaddrinfo.c, lib/getdelim.c: * lib/glob.c, lib/random_r.c, lib/setenv.c, lib/tsearch.c: * lib/unsetenv.c (_GL_ARG_NONNULL): Define before including <config.h>. GNU Emacs's <config.h> includes <stdlib.h> (which is not a great idea but is too painful to fix right now), and without this gnulib change <stdlib.h> was defining _GL_ARG_NONNULL incorrectly when compiling unsetenv.c on Solaris 11. Fix the problem for unsetenv.c, and fix other similar occurrences.
author Paul Eggert <eggert@cs.ucla.edu>
date Mon, 11 Feb 2013 14:58:56 -0800
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16235
diff changeset
1 /* Copyright (C) 1992, 1995-2002, 2005-2013 Free Software Foundation, Inc.
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 This file is part of the GNU C Library.
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
4 This program is free software: you can redistribute it and/or modify
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4080
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
7 (at your option) any later version.
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4080
diff changeset
9 This program is distributed in the hope that it will be useful,
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
4440
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4080
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e58a1c05a6ba Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4080
diff changeset
12 GNU General Public License for more details.
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
14 You should have received a copy of the GNU General Public License
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
12422
f7842310a565 New module 'arg-nonnull'. Declare which arguments expect non-NULL values.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
17 /* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
f7842310a565 New module 'arg-nonnull'. Declare which arguments expect non-NULL values.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
18 optimizes away the name == NULL test below. */
f7842310a565 New module 'arg-nonnull'. Declare which arguments expect non-NULL values.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
19 #define _GL_ARG_NONNULL(params)
f7842310a565 New module 'arg-nonnull'. Declare which arguments expect non-NULL values.
Bruno Haible <bruno@clisp.org>
parents: 12421
diff changeset
20
17326
bb52d9cece01 unsetenv etc.: port to Solaris 11 + GNU Emacs
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
21 #include <config.h>
bb52d9cece01 unsetenv etc.: port to Solaris 11 + GNU Emacs
Paul Eggert <eggert@cs.ucla.edu>
parents: 17249
diff changeset
22
9545
c596ca4e89b4 Split setenv module into setenv and unsetenv. Get rid of setenv.h.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
23 /* Specification. */
c596ca4e89b4 Split setenv module into setenv and unsetenv. Get rid of setenv.h.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
24 #include <stdlib.h>
c596ca4e89b4 Split setenv module into setenv and unsetenv. Get rid of setenv.h.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
25
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <errno.h>
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #if !_LIBC
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 # define __set_errno(ev) ((errno) = (ev))
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #endif
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
4683
4452e4c45cc4 Assume ANSI C <string.h>, <stdlib.h>.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
31 #include <string.h>
6659
54c2c5912cdc undo last change -- this file is sync'd from gettext
Jim Meyering <jim@meyering.net>
parents: 6650
diff changeset
32 #include <unistd.h>
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #if !_LIBC
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
35 # define __environ environ
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #endif
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 #if _LIBC
16235
18a38c9615f0 In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
39 /* This lock protects against simultaneous modifications of 'environ'. */
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 # include <bits/libc-lock.h>
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 __libc_lock_define_initialized (static, envlock)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
42 # define LOCK __libc_lock_lock (envlock)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
43 # define UNLOCK __libc_lock_unlock (envlock)
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 #else
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 # define LOCK
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 # define UNLOCK
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 #endif
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 /* In the GNU C library we must keep the namespace clean. */
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #ifdef _LIBC
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 # define unsetenv __unsetenv
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 #endif
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
12300
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
54 #if _LIBC || !HAVE_UNSETENV
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 int
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 unsetenv (const char *name)
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 size_t len;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 char **ep;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 {
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 __set_errno (EINVAL);
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 return -1;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 }
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 len = strlen (name);
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 LOCK;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 ep = __environ;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 while (*ep != NULL)
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
76 /* Found it. Remove this pointer by moving later ones back. */
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
77 char **dp = ep;
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
79 do
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
80 dp[0] = dp[1];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
81 while (*dp++);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 12307
diff changeset
82 /* Continue the loop in case NAME appears again. */
4080
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 }
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 else
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 ++ep;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 UNLOCK;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 return 0;
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 }
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 #ifdef _LIBC
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 # undef unsetenv
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 weak_alias (__unsetenv, unsetenv)
d064e5107035 setenv and unsetenv.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 #endif
12300
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
96
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
97 #else /* HAVE_UNSETENV */
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
98
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
99 # undef unsetenv
14867
355e7137b2de unsetenv: Avoid gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
100 # if !HAVE_DECL_UNSETENV
355e7137b2de unsetenv: Avoid gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
101 # if VOID_UNSETENV
355e7137b2de unsetenv: Avoid gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
102 extern void unsetenv (const char *);
355e7137b2de unsetenv: Avoid gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
103 # else
355e7137b2de unsetenv: Avoid gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
104 extern int unsetenv (const char *);
355e7137b2de unsetenv: Avoid gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
105 # endif
355e7137b2de unsetenv: Avoid gcc warning.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
106 # endif
12300
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
107
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
108 /* Call the underlying unsetenv, in case there is hidden bookkeeping
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
109 that needs updating beyond just modifying environ. */
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
110 int
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
111 rpl_unsetenv (const char *name)
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
112 {
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
113 int result = 0;
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
114 if (!name || !*name || strchr (name, '='))
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
115 {
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
116 errno = EINVAL;
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
117 return -1;
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
118 }
12307
a15e71d9f0b6 unsetenv: work around Solaris bug
Eric Blake <ebb9@byu.net>
parents: 12300
diff changeset
119 while (getenv (name))
12300
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
120 # if !VOID_UNSETENV
12307
a15e71d9f0b6 unsetenv: work around Solaris bug
Eric Blake <ebb9@byu.net>
parents: 12300
diff changeset
121 result =
12300
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
122 # endif
12307
a15e71d9f0b6 unsetenv: work around Solaris bug
Eric Blake <ebb9@byu.net>
parents: 12300
diff changeset
123 unsetenv (name);
12300
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
124 return result;
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
125 }
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
126
c8288fd3f281 setenv, unsetenv: work around various bugs
Eric Blake <ebb9@byu.net>
parents: 9721
diff changeset
127 #endif /* HAVE_UNSETENV */