Line | |
---|
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: /cvsup/minix/src/lib/other/putenv.c,v 1.1.1.1.8.1 2006/05/03 21:13:45 beng Exp $ */
|
---|
6 |
|
---|
7 | #include <stdlib.h>
|
---|
8 | #include <string.h>
|
---|
9 |
|
---|
10 | #define ENTRY_INC 10
|
---|
11 | #define rounded(x) (((x / ENTRY_INC) + 1) * ENTRY_INC)
|
---|
12 |
|
---|
13 | extern _CONST char ***_penviron;
|
---|
14 |
|
---|
15 | int
|
---|
16 | putenv(name)
|
---|
17 | char *name;
|
---|
18 | {
|
---|
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].
|
---|
25 | */
|
---|
26 |
|
---|
27 | if (!name) return 0;
|
---|
28 | if (*_penviron == NULL) return 1;
|
---|
29 | if (r = strchr(name, '=')) {
|
---|
30 | register _CONST char *p, *q;
|
---|
31 |
|
---|
32 | *r = '\0';
|
---|
33 |
|
---|
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.
|
---|
44 | */
|
---|
45 | *r = '=';
|
---|
46 | *v = name;
|
---|
47 | return 0;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 | *r = '=';
|
---|
52 | v = *_penviron;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if (!size) {
|
---|
56 | register _CONST char **p;
|
---|
57 | register int i = 0;
|
---|
58 |
|
---|
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;
|
---|
74 | }
|
---|
75 | v[size - 1] = name;
|
---|
76 | v[size] = NULL;
|
---|
77 | size++;
|
---|
78 | return 0;
|
---|
79 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.