Uninitialized vector entry?
[minix3.git] / lib / other / putenv.c
blob455ad5c5b0e5b6f50cdb2c0a1e302af1bc940bf7
1 /*
2 * (c) copyright 1989 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
4 */
5 /* $Header$ */
7 #include <stdlib.h>
8 #include <string.h>
10 #define ENTRY_INC 10
11 #define rounded(x) (((x / ENTRY_INC) + 1) * ENTRY_INC)
13 extern _CONST char ***_penviron;
15 int
16 putenv(name)
17 char *name;
19 register _CONST char **v = *_penviron;
20 register char *r;
21 static int size = 0;
22 /* When size != 0, it contains the number of entries in the
23 * table (including the final NULL pointer). This means that the
24 * last non-null entry is environ[size - 2].
27 if (!name) return 0;
28 if (*_penviron == NULL) return 1;
29 if (r = strchr(name, '=')) {
30 register _CONST char *p, *q;
32 *r = '\0';
34 if (v != NULL) {
35 while ((p = *v) != NULL) {
36 q = name;
37 while (*q && (*q++ == *p++))
38 /* EMPTY */ ;
39 if (*q || (*p != '=')) {
40 v++;
41 } else {
42 /* The name was already in the
43 * environment.
45 *r = '=';
46 *v = name;
47 return 0;
51 *r = '=';
52 v = *_penviron;
55 if (!size) {
56 register _CONST char **p;
57 register int i = 0;
59 if (v)
60 do {
61 i++;
62 } while (*v++);
63 if (!(v = malloc(rounded(i) * sizeof(char **))))
64 return 1;
65 size = i;
66 p = *_penviron;
67 *_penviron = v;
68 while (*v++ = *p++); /* copy the environment */
69 v = *_penviron;
70 } else if (!(size % ENTRY_INC)) {
71 if (!(v = realloc(*_penviron, rounded(size) * sizeof(char **))))
72 return 1;
73 *_penviron = v;
75 v[size - 1] = name;
76 v[size] = NULL;
77 size++;
78 return 0;