diff options
author | millert <> | 2005-02-16 21:20:22 +0000 |
---|---|---|
committer | millert <> | 2005-02-16 21:20:22 +0000 |
commit | 6df12cc08611edf2529319130a399001833c2319 (patch) | |
tree | 60657b7024404b06461e800f1dc433d535ebf404 | |
parent | 3273bfcfafdb256c9dbd7cbc504d9e52378982fd (diff) | |
download | openbsd-6df12cc08611edf2529319130a399001833c2319.tar.gz openbsd-6df12cc08611edf2529319130a399001833c2319.tar.bz2 openbsd-6df12cc08611edf2529319130a399001833c2319.zip |
Stash the environment pointer we get from realloc() instead of just
setting a flag. That way when we are called again we can be sure
to realloc() the right thing, regardless of the current value of
environ. When the stashed value != environ (or when we are called
for the first time), copy the existing entries from environ and set
environ to the new value. OK deraadt@, beck@, djm@
-rw-r--r-- | src/lib/libc/stdlib/setenv.c | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c index 2ba072b65c..40305dbe1c 100644 --- a/src/lib/libc/stdlib/setenv.c +++ b/src/lib/libc/stdlib/setenv.c | |||
@@ -28,7 +28,7 @@ | |||
28 | */ | 28 | */ |
29 | 29 | ||
30 | #if defined(LIBC_SCCS) && !defined(lint) | 30 | #if defined(LIBC_SCCS) && !defined(lint) |
31 | static char *rcsid = "$OpenBSD: setenv.c,v 1.6 2003/06/02 20:18:38 millert Exp $"; | 31 | static char *rcsid = "$OpenBSD: setenv.c,v 1.7 2005/02/16 21:20:22 millert Exp $"; |
32 | #endif /* LIBC_SCCS and not lint */ | 32 | #endif /* LIBC_SCCS and not lint */ |
33 | 33 | ||
34 | #include <stdlib.h> | 34 | #include <stdlib.h> |
@@ -36,6 +36,8 @@ static char *rcsid = "$OpenBSD: setenv.c,v 1.6 2003/06/02 20:18:38 millert Exp $ | |||
36 | 36 | ||
37 | char *__findenv(const char *name, int *offset); | 37 | char *__findenv(const char *name, int *offset); |
38 | 38 | ||
39 | extern char **environ; | ||
40 | |||
39 | /* | 41 | /* |
40 | * setenv -- | 42 | * setenv -- |
41 | * Set the value of the environmental variable "name" to be | 43 | * Set the value of the environmental variable "name" to be |
@@ -43,13 +45,12 @@ char *__findenv(const char *name, int *offset); | |||
43 | */ | 45 | */ |
44 | int | 46 | int |
45 | setenv(name, value, rewrite) | 47 | setenv(name, value, rewrite) |
46 | register const char *name; | 48 | const char *name; |
47 | register const char *value; | 49 | const char *value; |
48 | int rewrite; | 50 | int rewrite; |
49 | { | 51 | { |
50 | extern char **environ; | 52 | static char **lastenv; /* last value of environ */ |
51 | static int alloced; /* if allocated space before */ | 53 | char *C; |
52 | register char *C; | ||
53 | int l_value, offset; | 54 | int l_value, offset; |
54 | 55 | ||
55 | if (*value == '=') /* no `=' in value */ | 56 | if (*value == '=') /* no `=' in value */ |
@@ -64,30 +65,23 @@ setenv(name, value, rewrite) | |||
64 | return (0); | 65 | return (0); |
65 | } | 66 | } |
66 | } else { /* create new slot */ | 67 | } else { /* create new slot */ |
67 | register int cnt; | 68 | size_t cnt; |
68 | register char **P; | 69 | char **P; |
69 | 70 | ||
70 | for (P = environ, cnt = 0; *P; ++P, ++cnt); | 71 | for (P = environ; *P != NULL; P++) |
71 | if (alloced) { /* just increase size */ | 72 | ; |
72 | P = (char **)realloc((void *)environ, | 73 | cnt = P - environ; |
73 | (size_t)(sizeof(char *) * (cnt + 2))); | 74 | P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2)); |
74 | if (!P) | 75 | if (!P) |
75 | return (-1); | 76 | return (-1); |
76 | environ = P; | 77 | if (lastenv != environ) |
77 | } | 78 | memcpy(P, environ, cnt * sizeof(char *)); |
78 | else { /* get new space */ | 79 | lastenv = environ = P; |
79 | alloced = 1; /* copy old entries into it */ | ||
80 | P = (char **)malloc((size_t)(sizeof(char *) * | ||
81 | (cnt + 2))); | ||
82 | if (!P) | ||
83 | return (-1); | ||
84 | bcopy(environ, P, cnt * sizeof(char *)); | ||
85 | environ = P; | ||
86 | } | ||
87 | environ[cnt + 1] = NULL; | ||
88 | offset = cnt; | 80 | offset = cnt; |
81 | environ[cnt + 1] = NULL; | ||
89 | } | 82 | } |
90 | for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ | 83 | for (C = (char *)name; *C && *C != '='; ++C) |
84 | ; /* no `=' in name */ | ||
91 | if (!(environ[offset] = /* name + `=' + value */ | 85 | if (!(environ[offset] = /* name + `=' + value */ |
92 | malloc((size_t)((int)(C - name) + l_value + 2)))) | 86 | malloc((size_t)((int)(C - name) + l_value + 2)))) |
93 | return (-1); | 87 | return (-1); |
@@ -104,14 +98,12 @@ setenv(name, value, rewrite) | |||
104 | */ | 98 | */ |
105 | void | 99 | void |
106 | unsetenv(name) | 100 | unsetenv(name) |
107 | const char *name; | 101 | const char *name; |
108 | { | 102 | { |
109 | extern char **environ; | 103 | char **P; |
110 | register char **P; | ||
111 | int offset; | 104 | int offset; |
112 | char *__findenv(); | ||
113 | 105 | ||
114 | while (__findenv(name, &offset)) /* if set multiple times */ | 106 | while (__findenv(name, &offset)) /* if set multiple times */ |
115 | for (P = &environ[offset];; ++P) | 107 | for (P = &environ[offset];; ++P) |
116 | if (!(*P = *(P + 1))) | 108 | if (!(*P = *(P + 1))) |
117 | break; | 109 | break; |