Only reference environ indirectly through a pointer

This commit is contained in:
Michael Meissner 2000-11-22 18:26:10 +00:00
parent 0217c5bb3b
commit 73dea7905c
7 changed files with 67 additions and 25 deletions

View File

@ -1,3 +1,21 @@
2000-11-22 Michael Meissner <meissner@redhat.com>
* libc/posix/execl.c (execl): Don't reference environ directly,
reference it only via a static pointer to avoid problems with some
shared library systems and with different uses of small data where
the user specifies his own version of environ.
* libc/posix/execv.c (execv): Ditto.
* libc/stdlib/getenv_r.c (_findenv_r): Ditto.
* libc/stdlib/setenv_r.c (_setenv_r,_unsetenv_r): Ditto.
* libc/stdlib/system.c (system, !cygwin32 case): Ditto.
* libc/stdlib/getenv.c (environ): Delete unused reference to
environ.
* libc/stdlib/getenv_r.c: Make initial comment friendlier to emacs
colorization.
* libc/stdlib/system.c: Ditto.
Tue Nov 21 20:32:21 2000 Christopher Faylor <cgf@cygnus.com> Tue Nov 21 20:32:21 2000 Christopher Faylor <cgf@cygnus.com>
* libc/sys/cygwin/sys/dirent.h: Change definition to avoid necessity of * libc/sys/cygwin/sys/dirent.h: Change definition to avoid necessity of

View File

@ -6,6 +6,11 @@
#include <_ansi.h> #include <_ansi.h>
#include <unistd.h> #include <unistd.h>
/* Only deal with a pointer to environ, to work around subtle bugs with shared
libraries and/or small data systems where the user declares his own
'environ'. */
static char ***p_environ = &environ;
#ifdef _HAVE_STDC #ifdef _HAVE_STDC
#include <stdarg.h> #include <stdarg.h>
@ -38,5 +43,5 @@ execl (path, arg0, va_alist)
while (argv[i++] != NULL); while (argv[i++] != NULL);
va_end (args); va_end (args);
return _execve (path, (char * _CONST *) argv, environ); return _execve (path, (char * _CONST *) argv, *p_environ);
} }

View File

@ -6,10 +6,15 @@
#include <_ansi.h> #include <_ansi.h>
#include <unistd.h> #include <unistd.h>
/* Only deal with a pointer to environ, to work around subtle bugs with shared
libraries and/or small data systems where the user declares his own
'environ'. */
static char ***p_environ = &environ;
int int
_DEFUN (execv, (path, argv), _DEFUN (execv, (path, argv),
const char *path _AND const char *path _AND
char * const argv[]) char * const argv[])
{ {
return _execve (path, (char * _CONST *) argv, environ); return _execve (path, (char * _CONST *) argv, *p_environ);
} }

View File

@ -18,7 +18,7 @@ TRAD_SYNOPSIS
DESCRIPTION DESCRIPTION
<<getenv>> searches the list of environment variable names and values <<getenv>> searches the list of environment variable names and values
(using the global pointer `<<char **environ>>') for a variable whose (using the global pointer ``<<char **environ>>'') for a variable whose
name matches the string at <[name]>. If a variable name matches, name matches the string at <[name]>. If a variable name matches,
<<getenv>> returns a pointer to the associated value. <<getenv>> returns a pointer to the associated value.
@ -39,7 +39,7 @@ variables vary from one system to another.
*/ */
/* /*
* Copyright (c) 1987 Regents of the University of California. * Copyright (c) 1987, 2000 Regents of the University of California.
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms are permitted * Redistribution and use in source and binary forms are permitted
@ -63,8 +63,6 @@ variables vary from one system to another.
#include <stddef.h> #include <stddef.h>
#include <string.h> #include <string.h>
extern char **environ;
/* /*
* _findenv -- * _findenv --
* Returns pointer to value associated with name, if any, else NULL. * Returns pointer to value associated with name, if any, else NULL.

View File

@ -19,7 +19,7 @@ TRAD_SYNOPSIS
DESCRIPTION DESCRIPTION
<<_getenv_r>> searches the list of environment variable names and values <<_getenv_r>> searches the list of environment variable names and values
(using the global pointer `<<char **environ>>') for a variable whose (using the global pointer ``<<char **environ>>'') for a variable whose
name matches the string at <[name]>. If a variable name matches, name matches the string at <[name]>. If a variable name matches,
<<_getenv_r>> returns a pointer to the associated value. <<_getenv_r>> returns a pointer to the associated value.
@ -65,6 +65,11 @@ variables vary from one system to another.
extern char **environ; extern char **environ;
/* Only deal with a pointer to environ, to work around subtle bugs with shared
libraries and/or small data systems where the user declares his own
'environ'. */
static char ***p_environ = &environ;
/* /*
* _findenv -- * _findenv --
* Returns pointer to value associated with name, if any, else NULL. * Returns pointer to value associated with name, if any, else NULL.
@ -89,7 +94,7 @@ _DEFUN (_findenv_r, (reent_ptr, name, offset),
/* In some embedded systems, this does not get set. This protects /* In some embedded systems, this does not get set. This protects
newlib from dereferencing a bad pointer. */ newlib from dereferencing a bad pointer. */
if (!environ) if (!*p_environ)
return NULL; return NULL;
c = name; c = name;
@ -100,11 +105,11 @@ _DEFUN (_findenv_r, (reent_ptr, name, offset),
len++; len++;
} }
for (p = environ; *p; ++p) for (p = *p_environ; *p; ++p)
if (!strncmp (*p, name, len)) if (!strncmp (*p, name, len))
if (*(c = *p + len) == '=') if (*(c = *p + len) == '=')
{ {
*offset = p - environ; *offset = p - *p_environ;
ENV_UNLOCK; ENV_UNLOCK;
return (char *) (++c); return (char *) (++c);
} }

View File

@ -29,6 +29,13 @@
#include <string.h> #include <string.h>
#include "envlock.h" #include "envlock.h"
extern char **environ;
/* Only deal with a pointer to environ, to work around subtle bugs with shared
libraries and/or small data systems where the user declares his own
'environ'. */
static char ***p_environ = &environ;
/* _findenv_r is defined in getenv_r.c. */ /* _findenv_r is defined in getenv_r.c. */
extern char *_findenv_r _PARAMS ((struct _reent *, const char *, int *)); extern char *_findenv_r _PARAMS ((struct _reent *, const char *, int *));
@ -45,7 +52,6 @@ _DEFUN (_setenv_r, (reent_ptr, name, value, rewrite),
_CONST char *value _AND _CONST char *value _AND
int rewrite) int rewrite)
{ {
extern char **environ;
static int alloced; /* if allocated space before */ static int alloced; /* if allocated space before */
register char *C; register char *C;
int l_value, offset; int l_value, offset;
@ -74,12 +80,12 @@ _DEFUN (_setenv_r, (reent_ptr, name, value, rewrite),
register int cnt; register int cnt;
register char **P; register char **P;
for (P = environ, cnt = 0; *P; ++P, ++cnt); for (P = *p_environ, cnt = 0; *P; ++P, ++cnt);
if (alloced) if (alloced)
{ /* just increase size */ { /* just increase size */
environ = (char **) _realloc_r (reent_ptr, (char *) environ, *p_environ = (char **) _realloc_r (reent_ptr, (char *) environ,
(size_t) (sizeof (char *) * (cnt + 2))); (size_t) (sizeof (char *) * (cnt + 2)));
if (!environ) if (!*p_environ)
{ {
ENV_UNLOCK; ENV_UNLOCK;
return -1; return -1;
@ -94,20 +100,20 @@ _DEFUN (_setenv_r, (reent_ptr, name, value, rewrite),
ENV_UNLOCK; ENV_UNLOCK;
return (-1); return (-1);
} }
bcopy ((char *) environ, (char *) P, cnt * sizeof (char *)); bcopy ((char *) *p_environ, (char *) P, cnt * sizeof (char *));
environ = P; *p_environ = P;
} }
environ[cnt + 1] = NULL; (*p_environ)[cnt + 1] = NULL;
offset = cnt; offset = cnt;
} }
for (C = (char *) name; *C && *C != '='; ++C); /* no `=' in name */ for (C = (char *) name; *C && *C != '='; ++C); /* no `=' in name */
if (!(environ[offset] = /* name + `=' + value */ if (!((*p_environ)[offset] = /* name + `=' + value */
_malloc_r (reent_ptr, (size_t) ((int) (C - name) + l_value + 2)))) _malloc_r (reent_ptr, (size_t) ((int) (C - name) + l_value + 2))))
{ {
ENV_UNLOCK; ENV_UNLOCK;
return -1; return -1;
} }
for (C = environ[offset]; (*C = *name++) && *C != '='; ++C); for (C = (*p_environ)[offset]; (*C = *name++) && *C != '='; ++C);
for (*C++ = '='; (*C++ = *value++) != 0;); for (*C++ = '='; (*C++ = *value++) != 0;);
ENV_UNLOCK; ENV_UNLOCK;
@ -124,14 +130,13 @@ _DEFUN (_unsetenv_r, (reent_ptr, name),
struct _reent *reent_ptr _AND struct _reent *reent_ptr _AND
_CONST char *name) _CONST char *name)
{ {
extern char **environ;
register char **P; register char **P;
int offset; int offset;
ENV_LOCK; ENV_LOCK;
while (_findenv_r (reent_ptr, name, &offset)) /* if set multiple times */ while (_findenv_r (reent_ptr, name, &offset)) /* if set multiple times */
for (P = &environ[offset];; ++P) for (P = &(*p_environ)[offset];; ++P)
if (!(*P = *(P + 1))) if (!(*P = *(P + 1)))
break; break;

View File

@ -27,7 +27,7 @@ DESCRIPTION
Use <<system>> to pass a command string <<*<[s]>>> to <</bin/sh>> on Use <<system>> to pass a command string <<*<[s]>>> to <</bin/sh>> on
your system, and wait for it to finish executing. your system, and wait for it to finish executing.
Use `<<system(NULL)>>' to test whether your system has <</bin/sh>> Use ``<<system(NULL)>>'' to test whether your system has <</bin/sh>>
available. available.
The alternate function <<_system_r>> is a reentrant version. The The alternate function <<_system_r>> is a reentrant version. The
@ -107,6 +107,13 @@ system (s)
#endif #endif
#if defined (unix) && !defined (__CYGWIN__) #if defined (unix) && !defined (__CYGWIN__)
extern char **environ;
/* Only deal with a pointer to environ, to work around subtle bugs with shared
libraries and/or small data systems where the user declares his own
'environ'. */
static char ***p_environ = &environ;
static int static int
do_system (ptr, s) do_system (ptr, s)
struct _reent *ptr; struct _reent *ptr;
@ -114,7 +121,6 @@ do_system (ptr, s)
{ {
char *argv[4]; char *argv[4];
int pid, status; int pid, status;
extern char **environ;
argv[0] = "sh"; argv[0] = "sh";
argv[1] = "-c"; argv[1] = "-c";
@ -123,7 +129,7 @@ do_system (ptr, s)
if ((pid = _fork_r (ptr)) == 0) if ((pid = _fork_r (ptr)) == 0)
{ {
_execve ("/bin/sh", argv, environ); _execve ("/bin/sh", argv, *p_environ);
exit (100); exit (100);
} }
else if (pid == -1) else if (pid == -1)