From df3730247f93a1787d0ee43349ab60b1cda06dfb Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 30 Apr 2007 02:25:02 +0000 Subject: Make setenv, putenv, getenv and unsetenv conforming to Open Group specs Issue 6 (also IEEE Std 1003.1-2001) in following areas: args, return, errors. Putenv still needs rewriting because specs explicitly says that altering passed string later should change the environment (currently we copy the string so can't provide that). --- lib/libc/stdlib/setenv.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'lib/libc/stdlib/setenv.c') diff --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c index 202c022..4f05f32 100644 --- a/lib/libc/stdlib/setenv.c +++ b/lib/libc/stdlib/setenv.c @@ -33,6 +33,7 @@ static char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93"; #include __FBSDID("$FreeBSD$"); +#include #include #include #include @@ -55,6 +56,11 @@ setenv(name, value, rewrite) char *c; int l_value, offset; + if (name == NULL || !*name || strchr(name, '=') != NULL) { + errno = EINVAL; + return (-1); + } + if (*value == '=') /* no `=' in value */ ++value; l_value = strlen(value); @@ -88,9 +94,8 @@ setenv(name, value, rewrite) environ[cnt + 1] = NULL; offset = cnt; } - for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */ if (!(environ[offset] = /* name + `=' + value */ - malloc((size_t)((int)(c - name) + l_value + 2)))) + malloc((size_t)(strlen(name) + l_value + 2)))) return (-1); for (c = environ[offset]; (*c = *name++) && *c != '='; ++c); for (*c++ = '='; (*c++ = *value++); ); @@ -101,7 +106,7 @@ setenv(name, value, rewrite) * unsetenv(name) -- * Delete environmental variable "name". */ -void +int unsetenv(name) const char *name; { @@ -109,8 +114,14 @@ unsetenv(name) char **p; int offset; + if (name == NULL || !*name || strchr(name, '=') != NULL) { + errno = EINVAL; + return (-1); + } + while (__findenv(name, &offset)) /* if set multiple times */ for (p = &environ[offset];; ++p) if (!(*p = *(p + 1))) break; + return (0); } -- cgit v1.1