From 15fdb055e5a9a944685cf8bcf94e5858589a6740 Mon Sep 17 00:00:00 2001 From: "Andrey A. Chernov" Date: Mon, 30 Apr 2007 16:56:18 +0000 Subject: Make putenv() fully conforms to Open Group specs Issue 6 (also IEEE Std 1003.1-2001) The specs explicitly says that altering passed string should change the environment, i.e. putenv() directly puts its arg into environment (unlike setenv() which just copies it there). It means that putenv() can't be implemented via setenv() (like we have before) at all. Putenv() value lives (allows modifying) up to the next putenv() or setenv() call. --- lib/libc/stdlib/setenv.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lib/libc/stdlib/setenv.c') diff --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c index 2695af77b625..5725733b5066 100644 --- a/lib/libc/stdlib/setenv.c +++ b/lib/libc/stdlib/setenv.c @@ -38,6 +38,8 @@ __FBSDID("$FreeBSD$"); #include #include +char **__alloced; /* if allocated space before */ + char *__findenv(const char *, int *); /* @@ -52,7 +54,6 @@ setenv(name, value, rewrite) int rewrite; { extern char **environ; - static char **alloced; /* if allocated space before */ char *c; int l_value, offset; @@ -74,26 +75,25 @@ setenv(name, value, rewrite) char **p; for (p = environ, cnt = 0; *p; ++p, ++cnt); - if (alloced == environ) { /* just increase size */ + if (__alloced == environ) { /* just increase size */ p = (char **)realloc((char *)environ, (size_t)(sizeof(char *) * (cnt + 2))); if (!p) return (-1); - alloced = environ = p; } else { /* get new space */ /* copy old entries into it */ - p = malloc((size_t)(sizeof(char *) * (cnt + 2))); + p = (char **)malloc((size_t)(sizeof(char *) * (cnt + 2))); if (!p) return (-1); bcopy(environ, p, cnt * sizeof(char *)); - alloced = environ = p; } + __alloced = environ = p; environ[cnt + 1] = NULL; offset = cnt; } if (!(environ[offset] = /* name + `=' + value */ - malloc((size_t)(strlen(name) + l_value + 2)))) + (char *)malloc((size_t)(strlen(name) + l_value + 2)))) return (-1); for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); for (*c++ = '='; (*c++ = *value++); ); -- cgit v1.2.3