Mercurial > hg > octave-shane > gnulib-hg
annotate lib/putenv.c @ 8234:e488ab5dfa4f
Avoid conflicting types for 'unsetenv' on FreeBSD.
* lib/putenv.c (_unsetenv): Rename from "unsetenv", to avoid
conflicting with FreeBSD's (5.0 and 6.1) function declaration
in stdlib.h.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Sun, 25 Feb 2007 00:47:36 +0000 |
parents | 822e9380142e |
children | bbbbbf4cd1c5 |
rev | line source |
---|---|
8234
e488ab5dfa4f
Avoid conflicting types for 'unsetenv' on FreeBSD.
Jim Meyering <jim@meyering.net>
parents:
8214
diff
changeset
|
1 /* Copyright (C) 1991, 1994, 1997, 1998, 2000, 2003, 2004, 2005, 2006, 2007 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
2 Free Software 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 |
882 | 7 This program is free software; you can redistribute it and/or modify it |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 2, or (at your option) any | |
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 |
18 along with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5159
diff
changeset
|
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
20 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
21 #include <config.h> |
311 | 22 |
8214
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
23 /* undef putenv here, because some (e.g., Solaris 10) declare putenv in |
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
24 with a non-const argument. That would conflict with the declaration of |
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
25 rpl_putenv below (due to the #define putenv rpl_putenv from config.h). */ |
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
26 #undef putenv |
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
27 int rpl_putenv (char const *); |
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
28 |
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
|
29 #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
|
30 |
2411
90a1662ba7ec
Move inclusion of errno.h so it follows that of sys/types.h,
Jim Meyering <jim@meyering.net>
parents:
1570
diff
changeset
|
31 /* 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
|
32 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
|
33 #include <errno.h> |
5159 | 34 #ifndef __set_errno |
4364 | 35 # define __set_errno(ev) ((errno) = (ev)) |
36 #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
|
37 |
8214
822e9380142e
Minor fixups to port to Solaris 10 with Sun C 5.8.
Paul Eggert <eggert@cs.ucla.edu>
parents:
7302
diff
changeset
|
38 #include <stdlib.h> |
4672 | 39 #include <string.h> |
6275 | 40 #include <unistd.h> |
311 | 41 |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
42 #if HAVE_GNU_LD |
653 | 43 # define environ __environ |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
44 #else |
311 | 45 extern char **environ; |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
46 #endif |
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
47 |
4364 | 48 #if _LIBC |
49 /* This lock protects against simultaneous modifications of `environ'. */ | |
50 # include <bits/libc-lock.h> | |
51 __libc_lock_define_initialized (static, envlock) | |
52 # define LOCK __libc_lock_lock (envlock) | |
53 # define UNLOCK __libc_lock_unlock (envlock) | |
54 #else | |
55 # define LOCK | |
56 # define UNLOCK | |
57 #endif | |
58 | |
59 static int | |
8234
e488ab5dfa4f
Avoid conflicting types for 'unsetenv' on FreeBSD.
Jim Meyering <jim@meyering.net>
parents:
8214
diff
changeset
|
60 _unsetenv (const char *name) |
4364 | 61 { |
62 size_t len; | |
63 char **ep; | |
311 | 64 |
4364 | 65 if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) |
66 { | |
67 __set_errno (EINVAL); | |
68 return -1; | |
69 } | |
70 | |
71 len = strlen (name); | |
72 | |
73 LOCK; | |
74 | |
75 ep = environ; | |
76 while (*ep != NULL) | |
77 if (!strncmp (*ep, name, len) && (*ep)[len] == '=') | |
78 { | |
79 /* Found it. Remove this pointer by moving later ones back. */ | |
80 char **dp = ep; | |
81 | |
82 do | |
83 dp[0] = dp[1]; | |
84 while (*dp++); | |
85 /* Continue the loop in case NAME appears again. */ | |
86 } | |
87 else | |
88 ++ep; | |
89 | |
90 UNLOCK; | |
91 | |
92 return 0; | |
93 } | |
94 | |
95 | |
96 /* Put STRING, which is of the form "NAME=VALUE", in the environment. | |
97 If STRING contains no `=', then remove STRING from the environment. */ | |
9 | 98 int |
1556
44617e0f6ffe
Redefine putenv before including stdlib.h to work
Jim Meyering <jim@meyering.net>
parents:
1229
diff
changeset
|
99 rpl_putenv (const char *string) |
9 | 100 { |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
101 const char *const name_end = strchr (string, '='); |
9 | 102 register size_t size; |
103 register char **ep; | |
104 | |
105 if (name_end == NULL) | |
106 { | |
107 /* Remove the variable from the environment. */ | |
8234
e488ab5dfa4f
Avoid conflicting types for 'unsetenv' on FreeBSD.
Jim Meyering <jim@meyering.net>
parents:
8214
diff
changeset
|
108 return _unsetenv (string); |
9 | 109 } |
110 | |
111 size = 0; | |
311 | 112 for (ep = environ; *ep != NULL; ++ep) |
113 if (!strncmp (*ep, string, name_end - string) && | |
9 | 114 (*ep)[name_end - string] == '=') |
115 break; | |
116 else | |
117 ++size; | |
118 | |
119 if (*ep == NULL) | |
120 { | |
121 static char **last_environ = NULL; | |
311 | 122 char **new_environ = (char **) malloc ((size + 2) * sizeof (char *)); |
9 | 123 if (new_environ == NULL) |
124 return -1; | |
363
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
125 (void) memcpy ((void *) new_environ, (void *) environ, |
ec4b36233380
Get putenv.c from /copies (derived from glibc).
Jim Meyering <jim@meyering.net>
parents:
311
diff
changeset
|
126 size * sizeof (char *)); |
9 | 127 new_environ[size] = (char *) string; |
128 new_environ[size + 1] = NULL; | |
129 if (last_environ != NULL) | |
4364 | 130 free (last_environ); |
9 | 131 last_environ = new_environ; |
311 | 132 environ = new_environ; |
9 | 133 } |
134 else | |
135 *ep = (char *) string; | |
136 | |
137 return 0; | |
138 } |