1 /* Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
25 # if !defined(errno) && !defined(HAVE_ERRNO_DECL)
28 # define __set_errno(ev) ((errno) = (ev))
31 #if _LIBC || HAVE_STDLIB_H
34 #if _LIBC || HAVE_STRING_H
37 #if _LIBC || HAVE_UNISTD_H
42 # define __environ environ
43 # ifndef HAVE_ENVIRON_DECL
44 extern char **environ
;
49 /* This lock protects against simultaneous modifications of `environ'. */
50 # include <libc-lock.h>
51 __libc_lock_define_initialized (static, envlock
)
52 # define LOCK __libc_lock_lock (envlock)
53 # define UNLOCK __libc_lock_unlock (envlock)
59 /* In the GNU C library we must keep the namespace clean. */
61 # define clearenv __clearenv
65 /* If this variable is not a null pointer we allocated the current
67 static char **last_environ
;
71 setenv (name
, value
, replace
)
78 const size_t namelen
= strlen (name
);
79 const size_t vallen
= strlen (value
) + 1;
84 if (__environ
!= NULL
)
85 for (ep
= __environ
; *ep
!= NULL
; ++ep
)
86 if (!strncmp (*ep
, name
, namelen
) && (*ep
)[namelen
] == '=')
91 if (__environ
== NULL
|| *ep
== NULL
)
94 if (__environ
== last_environ
&& __environ
!= NULL
)
95 /* We allocated this space; we can extend it. */
96 new_environ
= (char **) realloc (last_environ
,
97 (size
+ 2) * sizeof (char *));
99 new_environ
= (char **) malloc ((size
+ 2) * sizeof (char *));
101 if (new_environ
== NULL
)
107 new_environ
[size
] = malloc (namelen
+ 1 + vallen
);
108 if (new_environ
[size
] == NULL
)
110 free ((char *) new_environ
);
111 __set_errno (ENOMEM
);
116 if (__environ
!= last_environ
)
117 memcpy ((char *) new_environ
, (char *) __environ
,
118 size
* sizeof (char *));
120 memcpy (new_environ
[size
], name
, namelen
);
121 new_environ
[size
][namelen
] = '=';
122 memcpy (&new_environ
[size
][namelen
+ 1], value
, vallen
);
124 new_environ
[size
+ 1] = NULL
;
126 last_environ
= __environ
= new_environ
;
130 size_t len
= strlen (*ep
);
131 if (len
+ 1 < namelen
+ 1 + vallen
)
133 /* The existing string is too short; malloc a new one. */
134 char *new = malloc (namelen
+ 1 + vallen
);
141 memcpy (*ep
, name
, namelen
);
142 (*ep
)[namelen
] = '=';
144 memcpy (&(*ep
)[namelen
+ 1], value
, vallen
);
156 const size_t len
= strlen (name
);
161 for (ep
= __environ
; *ep
; ++ep
)
162 if (!strncmp (*ep
, name
, len
) && (*ep
)[len
] == '=')
164 /* Found it. Remove this pointer by moving later ones back. */
169 /* Continue the loop in case NAME appears again. */
175 /* The `clearenv' was planned to be added to POSIX.1 but probably
176 never made it. Nevertheless the POSIX.9 standard (POSIX bindings
177 for Fortran 77) requires this function. */
183 if (__environ
== last_environ
&& __environ
!= NULL
)
185 /* We allocated this environment so we can free it. */
190 /* Clear the environment pointer removes the whole environment. */
199 weak_alias (__clearenv
, clearenv
)