Sun Jul 21 06:48:38 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>

* sysdeps/generic/setenv.c (__environ): Change conditional for #define
	from [!HAVE_GNU_LD] to [!_LIBC].
	[_LIBC]: Include <libc-lock.h> and define a lock.
	(LOCK, UNLOCK): New macros, no-ops for [! _LIBC].
	(setenv, unsetenv): Use them.
This commit is contained in:
Roland McGrath 1996-07-21 11:09:45 +00:00
parent c199a24feb
commit 9e0f40723e
1 changed files with 29 additions and 3 deletions

View File

@ -32,10 +32,21 @@ Cambridge, MA 02139, USA. */
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifndef HAVE_GNU_LD #if _LIBC - 0 == 0
#define __environ environ #define __environ environ
#endif #endif
#if _LIBC - 0
/* This lock protects against simultaneous modifications of `environ'. */
#include <libc-lock.h>
__libc_lock_define_initialized (static, envlock)
#define LOCK __libc_lock_lock (envlock)
#define UNLOCK __libc_lock_unlock (envlock)
#else
#define LOCK
#define UNLOCK
#endif
int int
setenv (name, value, replace) setenv (name, value, replace)
const char *name; const char *name;
@ -47,6 +58,8 @@ setenv (name, value, replace)
const size_t namelen = strlen (name); const size_t namelen = strlen (name);
const size_t vallen = strlen (value) + 1; const size_t vallen = strlen (value) + 1;
LOCK;
size = 0; size = 0;
for (ep = __environ; *ep != NULL; ++ep) for (ep = __environ; *ep != NULL; ++ep)
if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=') if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
@ -66,13 +79,17 @@ setenv (name, value, replace)
new_environ = (char **) malloc ((size + 2) * sizeof (char *)); new_environ = (char **) malloc ((size + 2) * sizeof (char *));
if (new_environ == NULL) if (new_environ == NULL)
return -1; {
UNLOCK;
return -1;
}
new_environ[size] = malloc (namelen + 1 + vallen); new_environ[size] = malloc (namelen + 1 + vallen);
if (new_environ[size] == NULL) if (new_environ[size] == NULL)
{ {
free ((char *) new_environ); free ((char *) new_environ);
errno = ENOMEM; errno = ENOMEM;
UNLOCK;
return -1; return -1;
} }
@ -96,7 +113,10 @@ setenv (name, value, replace)
/* The existing string is too short; malloc a new one. */ /* The existing string is too short; malloc a new one. */
char *new = malloc (namelen + 1 + vallen); char *new = malloc (namelen + 1 + vallen);
if (new == NULL) if (new == NULL)
return -1; {
UNLOCK;
return -1;
}
*ep = new; *ep = new;
} }
memcpy (*ep, name, namelen); memcpy (*ep, name, namelen);
@ -104,6 +124,8 @@ setenv (name, value, replace)
memcpy (&(*ep)[namelen + 1], value, vallen); memcpy (&(*ep)[namelen + 1], value, vallen);
} }
UNLOCK;
return 0; return 0;
} }
@ -114,6 +136,8 @@ unsetenv (name)
const size_t len = strlen (name); const size_t len = strlen (name);
char **ep; char **ep;
LOCK;
for (ep = __environ; *ep; ++ep) for (ep = __environ; *ep; ++ep)
if (!strncmp (*ep, name, len) && (*ep)[len] == '=') if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
{ {
@ -124,4 +148,6 @@ unsetenv (name)
while (*dp++); while (*dp++);
/* Continue the loop in case NAME appears again. */ /* Continue the loop in case NAME appears again. */
} }
UNLOCK;
} }