diff options
author | millert <> | 2016-04-25 21:36:04 +0000 |
---|---|---|
committer | millert <> | 2016-04-25 21:36:04 +0000 |
commit | cba9688121bd9625551d439dc616413d95405943 (patch) | |
tree | 2aa15cd3630d409165e7cdf43a2ca28c734f94ae /src | |
parent | 4c9957c9640eb1eeb33d78bfecc78449eea8ba15 (diff) | |
download | openbsd-cba9688121bd9625551d439dc616413d95405943.tar.gz openbsd-cba9688121bd9625551d439dc616413d95405943.tar.bz2 openbsd-cba9688121bd9625551d439dc616413d95405943.zip |
Allow setenv(3) and putenv(3) to operate on a NULL environ pointer.
The getenv(3) and unsetenv(3) functions already support this.
This will make it easier to emulate the glibc clearenv() function in ports.
Based on a diff from and OK jca@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/setenv.c | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c index 1182abdaa3..60c2cd46a6 100644 --- a/src/lib/libc/stdlib/setenv.c +++ b/src/lib/libc/stdlib/setenv.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: setenv.c,v 1.17 2016/03/13 18:34:21 guenther Exp $ */ | 1 | /* $OpenBSD: setenv.c,v 1.18 2016/04/25 21:36:04 millert Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 1987 Regents of the University of California. | 3 | * Copyright (c) 1987 Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
@@ -43,7 +43,7 @@ int | |||
43 | putenv(char *str) | 43 | putenv(char *str) |
44 | { | 44 | { |
45 | char **P, *cp; | 45 | char **P, *cp; |
46 | size_t cnt; | 46 | size_t cnt = 0; |
47 | int offset = 0; | 47 | int offset = 0; |
48 | 48 | ||
49 | for (cp = str; *cp && *cp != '='; ++cp) | 49 | for (cp = str; *cp && *cp != '='; ++cp) |
@@ -65,13 +65,15 @@ putenv(char *str) | |||
65 | } | 65 | } |
66 | 66 | ||
67 | /* create new slot for string */ | 67 | /* create new slot for string */ |
68 | for (P = environ; *P != NULL; P++) | 68 | if (environ != NULL) { |
69 | ; | 69 | for (P = environ; *P != NULL; P++) |
70 | cnt = P - environ; | 70 | ; |
71 | cnt = P - environ; | ||
72 | } | ||
71 | P = reallocarray(lastenv, cnt + 2, sizeof(char *)); | 73 | P = reallocarray(lastenv, cnt + 2, sizeof(char *)); |
72 | if (!P) | 74 | if (!P) |
73 | return (-1); | 75 | return (-1); |
74 | if (lastenv != environ) | 76 | if (lastenv != environ && environ != NULL) |
75 | memcpy(P, environ, cnt * sizeof(char *)); | 77 | memcpy(P, environ, cnt * sizeof(char *)); |
76 | lastenv = environ = P; | 78 | lastenv = environ = P; |
77 | environ[cnt] = str; | 79 | environ[cnt] = str; |
@@ -122,15 +124,17 @@ setenv(const char *name, const char *value, int rewrite) | |||
122 | break; | 124 | break; |
123 | } | 125 | } |
124 | } else { /* create new slot */ | 126 | } else { /* create new slot */ |
125 | size_t cnt; | 127 | size_t cnt = 0; |
126 | 128 | ||
127 | for (P = environ; *P != NULL; P++) | 129 | if (environ != NULL) { |
128 | ; | 130 | for (P = environ; *P != NULL; P++) |
129 | cnt = P - environ; | 131 | ; |
132 | cnt = P - environ; | ||
133 | } | ||
130 | P = reallocarray(lastenv, cnt + 2, sizeof(char *)); | 134 | P = reallocarray(lastenv, cnt + 2, sizeof(char *)); |
131 | if (!P) | 135 | if (!P) |
132 | return (-1); | 136 | return (-1); |
133 | if (lastenv != environ) | 137 | if (lastenv != environ && environ != NULL) |
134 | memcpy(P, environ, cnt * sizeof(char *)); | 138 | memcpy(P, environ, cnt * sizeof(char *)); |
135 | lastenv = environ = P; | 139 | lastenv = environ = P; |
136 | offset = cnt; | 140 | offset = cnt; |