summaryrefslogtreecommitdiffstats
path: root/lib/libc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc')
-rw-r--r--lib/libc/stdlib/getenv.353
-rw-r--r--lib/libc/stdlib/getenv.c7
-rw-r--r--lib/libc/stdlib/putenv.c48
-rw-r--r--lib/libc/stdlib/setenv.c29
4 files changed, 41 insertions, 96 deletions
diff --git a/lib/libc/stdlib/getenv.3 b/lib/libc/stdlib/getenv.3
index 9ebf30b..3d365f1 100644
--- a/lib/libc/stdlib/getenv.3
+++ b/lib/libc/stdlib/getenv.3
@@ -32,7 +32,7 @@
.\" @(#)getenv.3 8.2 (Berkeley) 12/11/93
.\" $FreeBSD$
.\"
-.Dd April 30, 2007
+.Dd October 12, 2006
.Dt GETENV 3
.Os
.Sh NAME
@@ -50,13 +50,22 @@
.Ft int
.Fn setenv "const char *name" "const char *value" "int overwrite"
.Ft int
-.Fn putenv "char *string"
-.Ft int
+.Fn putenv "const char *string"
+.Ft void
.Fn unsetenv "const char *name"
.Sh DESCRIPTION
These functions set, unset and fetch environment variables from the
host
.Em environment list .
+For compatibility with differing environment conventions,
+the given arguments
+.Fa name
+and
+.Fa value
+may be appended and prepended,
+respectively,
+with an equal sign
+.Dq Li \&= .
.Pp
The
.Fn getenv
@@ -88,18 +97,11 @@ to the given
.Pp
The
.Fn putenv
-function takes an argument of the form ``name=value'' and
-puts it directly into the current environment,
-so altering the argument shall change the environment.
-If the variable
-.Fa name
-does not exist in the list,
-it is inserted with the given
-.Fa value .
-If the variable
-.Fa name
-does exist, it is reset to the given
-.Fa value .
+function takes an argument of the form ``name=value'' and is
+equivalent to:
+.Bd -literal -offset indent
+setenv(name, value, 1);
+.Ed
.Pp
The
.Fn unsetenv
@@ -119,21 +121,9 @@ is not in the current environment,
.Dv NULL
is returned.
.Pp
-.Rv -std setenv putenv unsetenv
+.Rv -std setenv putenv
.Sh ERRORS
.Bl -tag -width Er
-.It Bq Er EINVAL
-The function
-.Fn setenv
-or
-.Fn unsetenv
-failed because the
-.Fa name
-is a
-.Dv NULL
-pointer, points to an empty string, or points to a string containing an
-.Dq Li \&=
-character.
.It Bq Er ENOMEM
The function
.Fn setenv
@@ -151,13 +141,6 @@ The
.Fn getenv
function conforms to
.St -isoC .
-The
-.Fn setenv ,
-.Fn putenv
-and
-.Fn unsetenv
-functions conforms to
-.St -p1003.1-2001 .
.Sh HISTORY
The functions
.Fn setenv
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c
index 9ff18d8..306b6a1 100644
--- a/lib/libc/stdlib/getenv.c
+++ b/lib/libc/stdlib/getenv.c
@@ -43,7 +43,7 @@ inline char *__findenv(const char *, int *);
* __findenv --
* Returns pointer to value associated with name, if any, else NULL.
* Sets offset to be the offset of the name/value combination in the
- * environmental array, for use by putenv(3), setenv(3) and unsetenv(3).
+ * environmental array, for use by setenv(3) and unsetenv(3).
* Explicitly removes '=' in argument name.
*
* This routine *should* be a static; don't use it.
@@ -58,7 +58,7 @@ __findenv(name, offset)
const char *np;
char **p, *cp;
- if (environ == NULL)
+ if (name == NULL || environ == NULL)
return (NULL);
for (np = name; *np && *np != '='; ++np)
continue;
@@ -85,8 +85,5 @@ getenv(name)
{
int offset;
- if (name == NULL || !*name || strchr(name, '=') != NULL)
- return (NULL);
-
return (__findenv(name, &offset));
}
diff --git a/lib/libc/stdlib/putenv.c b/lib/libc/stdlib/putenv.c
index 56b78cf..a5eea5d 100644
--- a/lib/libc/stdlib/putenv.c
+++ b/lib/libc/stdlib/putenv.c
@@ -33,50 +33,24 @@ static char sccsid[] = "@(#)putenv.c 8.2 (Berkeley) 3/27/94";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
-#include <errno.h>
#include <stdlib.h>
#include <string.h>
-extern char **__alloced; /* if allocated space before */
-
-char *__findenv(const char *, int *);
-
int
putenv(str)
- char *str;
+ const char *str;
{
- extern char **environ;
- char *eq;
- int offset;
+ char *p, *equal;
+ int rval;
- if (str == NULL || (eq = strchr(str, '=')) == NULL || eq == str) {
- errno = EINVAL;
+ if ((p = strdup(str)) == NULL)
+ return (-1);
+ if ((equal = index(p, '=')) == NULL) {
+ (void)free(p);
return (-1);
}
-
- /* Trimmed version of setenv(3). */
- if (__findenv(str, &offset) == NULL) {
- int cnt;
- char **p;
-
- for (p = environ, cnt = 0; *p; ++p, ++cnt);
- if (__alloced == environ) { /* just increase size */
- p = (char **)realloc((char *)environ,
- (size_t)(sizeof(char *) * (cnt + 2)));
- if (!p)
- return (-1);
- }
- else { /* get new space */
- /* copy old entries into it */
- p = (char **)malloc((size_t)(sizeof(char *) * (cnt + 2)));
- if (!p)
- return (-1);
- bcopy(environ, p, cnt * sizeof(char *));
- }
- __alloced = environ = p;
- environ[cnt + 1] = NULL;
- offset = cnt;
- }
- environ[offset] = str;
- return (0);
+ *equal = '\0';
+ rval = setenv(p, equal + 1, 1);
+ (void)free(p);
+ return (rval);
}
diff --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c
index 5725733..202c022 100644
--- a/lib/libc/stdlib/setenv.c
+++ b/lib/libc/stdlib/setenv.c
@@ -33,13 +33,10 @@ static char sccsid[] = "@(#)setenv.c 8.1 (Berkeley) 6/4/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
-#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
-char **__alloced; /* if allocated space before */
-
char *__findenv(const char *, int *);
/*
@@ -54,14 +51,12 @@ setenv(name, value, rewrite)
int rewrite;
{
extern char **environ;
+ static char **alloced; /* if allocated space before */
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);
if ((c = __findenv(name, &offset))) { /* find if already exists */
if (!rewrite)
@@ -75,25 +70,27 @@ 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 = (char **)malloc((size_t)(sizeof(char *) * (cnt + 2)));
+ p = 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;
}
+ for (c = (char *)name; *c && *c != '='; ++c); /* no `=' in name */
if (!(environ[offset] = /* name + `=' + value */
- (char *)malloc((size_t)(strlen(name) + l_value + 2))))
+ malloc((size_t)((int)(c - name) + l_value + 2))))
return (-1);
for (c = environ[offset]; (*c = *name++) && *c != '='; ++c);
for (*c++ = '='; (*c++ = *value++); );
@@ -104,7 +101,7 @@ setenv(name, value, rewrite)
* unsetenv(name) --
* Delete environmental variable "name".
*/
-int
+void
unsetenv(name)
const char *name;
{
@@ -112,14 +109,8 @@ 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);
}
OpenPOWER on IntegriCloud