summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2005-02-16 21:20:23 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2005-02-16 21:20:23 +0000
commitf1bb225f28476909e1b5f37bd00186376627ede3 (patch)
treefb4bdceb50f55167c1c2d8c18699ed14e738f700 /lib
parent0fac444eb98c1e4bd2856ad3d6c45b2d8436f189 (diff)
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@
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdlib/setenv.c56
1 files changed, 24 insertions, 32 deletions
diff --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c
index 2ba072b65c6..40305dbe1c8 100644
--- a/lib/libc/stdlib/setenv.c
+++ b/lib/libc/stdlib/setenv.c
@@ -28,7 +28,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: setenv.c,v 1.6 2003/06/02 20:18:38 millert Exp $";
+static char *rcsid = "$OpenBSD: setenv.c,v 1.7 2005/02/16 21:20:22 millert Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdlib.h>
@@ -36,6 +36,8 @@ static char *rcsid = "$OpenBSD: setenv.c,v 1.6 2003/06/02 20:18:38 millert Exp $
char *__findenv(const char *name, int *offset);
+extern char **environ;
+
/*
* setenv --
* Set the value of the environmental variable "name" to be
@@ -43,13 +45,12 @@ char *__findenv(const char *name, int *offset);
*/
int
setenv(name, value, rewrite)
- register const char *name;
- register const char *value;
+ const char *name;
+ const char *value;
int rewrite;
{
- extern char **environ;
- static int alloced; /* if allocated space before */
- register char *C;
+ static char **lastenv; /* last value of environ */
+ char *C;
int l_value, offset;
if (*value == '=') /* no `=' in value */
@@ -64,30 +65,23 @@ setenv(name, value, rewrite)
return (0);
}
} else { /* create new slot */
- register int cnt;
- register char **P;
+ size_t cnt;
+ char **P;
- for (P = environ, cnt = 0; *P; ++P, ++cnt);
- if (alloced) { /* just increase size */
- P = (char **)realloc((void *)environ,
- (size_t)(sizeof(char *) * (cnt + 2)));
- if (!P)
- return (-1);
- environ = P;
- }
- else { /* get new space */
- alloced = 1; /* copy old entries into it */
- P = (char **)malloc((size_t)(sizeof(char *) *
- (cnt + 2)));
- if (!P)
- return (-1);
- bcopy(environ, P, cnt * sizeof(char *));
- environ = P;
- }
- environ[cnt + 1] = NULL;
+ for (P = environ; *P != NULL; P++)
+ ;
+ cnt = P - environ;
+ P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2));
+ if (!P)
+ return (-1);
+ if (lastenv != environ)
+ memcpy(P, environ, cnt * sizeof(char *));
+ lastenv = environ = P;
offset = cnt;
+ environ[cnt + 1] = NULL;
}
- for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */
+ for (C = (char *)name; *C && *C != '='; ++C)
+ ; /* no `=' in name */
if (!(environ[offset] = /* name + `=' + value */
malloc((size_t)((int)(C - name) + l_value + 2))))
return (-1);
@@ -104,14 +98,12 @@ setenv(name, value, rewrite)
*/
void
unsetenv(name)
- const char *name;
+ const char *name;
{
- extern char **environ;
- register char **P;
+ char **P;
int offset;
- char *__findenv();
- while (__findenv(name, &offset)) /* if set multiple times */
+ while (__findenv(name, &offset)) /* if set multiple times */
for (P = &environ[offset];; ++P)
if (!(*P = *(P + 1)))
break;