ack/lang/cem/libcc.ansi/misc/putenv.c

80 lines
1.5 KiB
C
Raw Normal View History

1990-01-03 17:23:10 +00:00
/*
* (c) copyright 1989 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
#include <stdlib.h>
#include <string.h>
#define ENTRY_INC 10
#define rounded(x) (((x / ENTRY_INC) + 1) * ENTRY_INC)
1990-09-27 16:52:07 +00:00
extern const char **_penvp;
extern const char **environ; /* environ is a shadow name for _penvp */
1990-01-03 17:23:10 +00:00
int
putenv(char *name)
{
1990-09-27 16:52:07 +00:00
register const char **v = _penvp;
1990-01-03 17:23:10 +00:00
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
1990-09-27 16:52:07 +00:00
* last non-null entry is _penvp[size - 2].
1990-01-03 17:23:10 +00:00
*/
if (!name) return 0;
1990-01-04 08:39:10 +00:00
if (r = strchr(name, '=')) {
1990-01-03 17:23:10 +00:00
register const char *p, *q;
*r = '\0';
if (v != NULL) {
while ((p = *v) != NULL) {
q = name;
while (*q && (*q++ == *p++))
/* EMPTY */ ;
if (*q || (*p != '=')) {
v++;
} else {
/* The name was already in the
* environment.
*/
*r = '=';
*v = name;
return 0;
}
}
}
*r = '=';
1990-09-27 16:52:07 +00:00
v = _penvp;
1990-01-03 17:23:10 +00:00
}
if (!size) {
register const char **p;
register int i = 0;
if (v)
do {
i++;
} while (*v++);
if (!(v = malloc(rounded(i) * sizeof(char **))))
return 1;
size = i;
1990-09-27 16:52:07 +00:00
p = _penvp;
_penvp = v;
1990-01-03 17:23:10 +00:00
while (*v++ = *p++); /* copy the environment */
1990-09-27 16:52:07 +00:00
v = _penvp;
1990-01-03 17:23:10 +00:00
} else if (!(size % ENTRY_INC)) {
1990-09-27 16:52:07 +00:00
if (!(v = realloc(_penvp, rounded(size) * sizeof(char **))))
1990-01-03 17:23:10 +00:00
return 1;
1990-09-27 16:52:07 +00:00
_penvp = v;
1990-01-03 17:23:10 +00:00
}
v[size - 1] = name;
v[size] = NULL;
size++;
1990-09-27 16:52:07 +00:00
environ = _penvp;
1990-01-03 17:23:10 +00:00
return 0;
}