changed environment handling to decrease namespace pollution

This commit is contained in:
eck 1990-09-27 13:40:08 +00:00
parent a6d0f40fc8
commit 81ce150a96
4 changed files with 35 additions and 11 deletions

View file

@ -11,6 +11,7 @@ getw.c
opendir.c
popen.c
putenv.c
environ.c
putw.c
readdir.c
rewinddir.c

View file

@ -1,4 +1,5 @@
clean:
rm -f getgrent.o getopt.o getpass.o getpw.o getw.o putw.o putenv.o \
popen.o sleep.o termcap.o fdopen.o closedir.o getdents.o \
opendir.o readdir.o rewinddir.o seekdir.o telldir.o OLIST
environ.o popen.o sleep.o termcap.o fdopen.o closedir.o \
getdents.o opendir.o readdir.o rewinddir.o seekdir.o \
telldir.o OLIST

View file

@ -0,0 +1,20 @@
/*
* environ.c - define the variable environ
*/
/* $Header$ */
/*
* This file defines the variable environ and initializes it with a magic
* value. The C run-time start-off routine tests whether the variable
* environ is initialized with this value. If it is not, it is assumed
* that it is defined by the user. Only two bytes are tested, since we
* don't know the endian-ness and alignment restrictions of the machine.
* This means that the low-order two-bytes should be equal to the
* high-order two-bytes on machines with four-byte pointers. In fact, all
* the bytes in the pointer are the same, just in case.
*/
#if _EM_PSIZE==2
char **environ = (char **) 0x5353;
#else
char **environ = (char **) 0x53535353;
#endif

View file

@ -10,17 +10,18 @@
#define ENTRY_INC 10
#define rounded(x) (((x / ENTRY_INC) + 1) * ENTRY_INC)
extern const char **environ;
extern const char **_envp;
extern const char **environ; /* environ is a shadow name for _envp */
int
putenv(char *name)
{
register const char **v = environ;
register const char **v = _envp;
register char *r;
static int size = 0;
/* When size != 0, it contains the number of entries in the
* table (including the final NULL pointer). This means that the
* last non-null entry is environ[size - 2].
* last non-null entry is _envp[size - 2].
*/
if (!name) return 0;
@ -47,7 +48,7 @@ putenv(char *name)
}
}
*r = '=';
v = environ;
v = _envp;
}
if (!size) {
@ -61,17 +62,18 @@ putenv(char *name)
if (!(v = malloc(rounded(i) * sizeof(char **))))
return 1;
size = i;
p = environ;
environ = v;
p = _envp;
_envp = v;
while (*v++ = *p++); /* copy the environment */
v = environ;
v = _envp;
} else if (!(size % ENTRY_INC)) {
if (!(v = realloc(environ, rounded(size) * sizeof(char **))))
if (!(v = realloc(_envp, rounded(size) * sizeof(char **))))
return 1;
environ = v;
_envp = v;
}
v[size - 1] = name;
v[size] = NULL;
size++;
environ = _envp;
return 0;
}