Mercurial > hg > octave-shane > gnulib-hg
annotate lib/putenv.c @ 17255:d81be792518a
update from texinfo
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Tue, 01 Jan 2013 15:51:49 -0800 |
parents | e542fd46ad6f |
children | a72ac603a92f |
rev | line source |
---|---|
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
16235
diff
changeset
|
1 /* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2013 Free Software |
12518
b5e42ef33b49
update nearly all FSF copyright year lists to include 2009
Jim Meyering <meyering@redhat.com>
parents:
12421
diff
changeset
|
2 Foundation, Inc. |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
3 |
882 | 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. | |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
6 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8234
diff
changeset
|
7 This program is free software: you can redistribute it and/or modify it |
882 | 8 under the terms of the GNU General Public License as published by the |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8234
diff
changeset
|
9 Free Software Foundation; either version 3 of the License, or any |
882 | 10 later version. |
311 | 11 |
882 | 12 This program 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 General Public License for more details. | |
9 | 16 |
882 | 17 You should have received a copy of the GNU General Public License |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8234
diff
changeset
|
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
20 #include <config.h> |
311 | 21 |
9434
b839560eec9f
Define the putenv substitute in <stdlib.h> rather than in <config.h>.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
22 /* Specification. */ |
b839560eec9f
Define the putenv substitute in <stdlib.h> rather than in <config.h>.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
23 #include <stdlib.h> |
8214
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
24 |
4347
df44e79ce676
.h files should stand alone, but we shouldn't include <sys/types.h>
Paul Eggert <eggert@cs.ucla.edu>
parents:
2411
diff
changeset
|
25 #include <stddef.h> |
1229
ed51f6d45a14
(sys/types.h): Include to get size_t on some systems.
Jim Meyering <jim@meyering.net>
parents:
959
diff
changeset
|
26 |
2411
90a1662ba7ec
Move inclusion of errno.h so it follows that of sys/types.h,
Jim Meyering <jim@meyering.net>
parents:
1570
diff
changeset
|
27 /* Include errno.h *after* sys/types.h to work around header problems |
90a1662ba7ec
Move inclusion of errno.h so it follows that of sys/types.h,
Jim Meyering <jim@meyering.net>
parents:
1570
diff
changeset
|
28 on AIX 3.2.5. */ |
90a1662ba7ec
Move inclusion of errno.h so it follows that of sys/types.h,
Jim Meyering <jim@meyering.net>
parents:
1570
diff
changeset
|
29 #include <errno.h> |
5159 | 30 #ifndef __set_errno |
4364 | 31 # define __set_errno(ev) ((errno) = (ev)) |
32 #endif | |
2411
90a1662ba7ec
Move inclusion of errno.h so it follows that of sys/types.h,
Jim Meyering <jim@meyering.net>
parents:
1570
diff
changeset
|
33 |
4672 | 34 #include <string.h> |
6275 | 35 #include <unistd.h> |
311 | 36 |
15639 | 37 #if _LIBC |
38 # if HAVE_GNU_LD | |
16020
af314a807a78
putenv: indent #definition of "environ" to placate cppi
Jim Meyering <meyering@redhat.com>
parents:
15639
diff
changeset
|
39 # define environ __environ |
15639 | 40 # else |
311 | 41 extern char **environ; |
15639 | 42 # endif |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
43 #endif |
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
44 |
4364 | 45 #if _LIBC |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
46 /* This lock protects against simultaneous modifications of 'environ'. */ |
4364 | 47 # include <bits/libc-lock.h> |
48 __libc_lock_define_initialized (static, envlock) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
49 # define LOCK __libc_lock_lock (envlock) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
50 # define UNLOCK __libc_lock_unlock (envlock) |
4364 | 51 #else |
52 # define LOCK | |
53 # define UNLOCK | |
54 #endif | |
55 | |
56 static int | |
8234
e488ab5dfa4f
Avoid conflicting types for 'unsetenv' on FreeBSD.
Jim Meyering <jim@meyering.net>
parents:
8214
diff
changeset
|
57 _unsetenv (const char *name) |
4364 | 58 { |
59 size_t len; | |
60 char **ep; | |
311 | 61 |
4364 | 62 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) |
63 { | |
64 __set_errno (EINVAL); | |
65 return -1; | |
66 } | |
67 | |
68 len = strlen (name); | |
69 | |
70 LOCK; | |
71 | |
72 ep = environ; | |
73 while (*ep != NULL) | |
74 if (!strncmp (*ep, name, len) && (*ep)[len] == '=') | |
75 { | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
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:
10384
diff
changeset
|
77 char **dp = ep; |
4364 | 78 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
79 do |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
80 dp[0] = dp[1]; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
81 while (*dp++); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
82 /* Continue the loop in case NAME appears again. */ |
4364 | 83 } |
84 else | |
85 ++ep; | |
86 | |
87 UNLOCK; | |
88 | |
89 return 0; | |
90 } | |
91 | |
92 | |
93 /* Put STRING, which is of the form "NAME=VALUE", in the environment. | |
16235
18a38c9615f0
In commentary, do not use ` to quote.
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
94 If STRING contains no '=', then remove STRING from the environment. */ |
9 | 95 int |
9434
b839560eec9f
Define the putenv substitute in <stdlib.h> rather than in <config.h>.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
96 putenv (char *string) |
9 | 97 { |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
98 const char *const name_end = strchr (string, '='); |
9 | 99 register size_t size; |
100 register char **ep; | |
101 | |
102 if (name_end == NULL) | |
103 { | |
104 /* Remove the variable from the environment. */ | |
8234
e488ab5dfa4f
Avoid conflicting types for 'unsetenv' on FreeBSD.
Jim Meyering <jim@meyering.net>
parents:
8214
diff
changeset
|
105 return _unsetenv (string); |
9 | 106 } |
107 | |
108 size = 0; | |
311 | 109 for (ep = environ; *ep != NULL; ++ep) |
110 if (!strncmp (*ep, string, name_end - string) && | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
111 (*ep)[name_end - string] == '=') |
9 | 112 break; |
113 else | |
114 ++size; | |
115 | |
116 if (*ep == NULL) | |
117 { | |
118 static char **last_environ = NULL; | |
311 | 119 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *)); |
9 | 120 if (new_environ == NULL) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
121 return -1; |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
122 (void) memcpy ((void *) new_environ, (void *) environ, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
10384
diff
changeset
|
123 size * sizeof (char *)); |
9 | 124 new_environ[size] = (char *) string; |
125 new_environ[size + 1] = NULL; | |
9749
daf5ba9fe2d9
Remove useless "if" tests before free. Deprecate "free" module.
Jim Meyering <meyering@redhat.com>
parents:
9434
diff
changeset
|
126 free (last_environ); |
9 | 127 last_environ = new_environ; |
311 | 128 environ = new_environ; |
9 | 129 } |
130 else | |
9434
b839560eec9f
Define the putenv substitute in <stdlib.h> rather than in <config.h>.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
131 *ep = string; |
9 | 132 |
133 return 0; | |
134 } |