From ba174a5e38b1ab764c4a36ef4dd83e80c4add148 Mon Sep 17 00:00:00 2001 From: "Andrey A. Chernov" Date: Tue, 1 May 2007 16:02:44 +0000 Subject: [PATCH] Back out all POSIXified *env() changes. Not because I admit they are technically wrong and not because of bug reports (I receive nothing). But because I surprisingly meets so strong opposition and resistance so lost any desire to continue that. Anyone who interested in POSIX can dig out what changes and how through cvs diffs. --- bin/df/df.c | 8 ++--- bin/sh/var.c | 13 +++------ include/stdlib.h | 4 +-- lib/libc/stdlib/getenv.3 | 53 ++++++++++++---------------------- lib/libc/stdlib/getenv.c | 7 ++--- lib/libc/stdlib/putenv.c | 48 +++++++----------------------- lib/libc/stdlib/setenv.c | 29 +++++++------------ libexec/pppoed/pppoed.c | 8 ++--- sys/sys/param.h | 2 +- usr.bin/du/du.c | 6 ++-- usr.bin/env/env.c | 6 ++-- usr.bin/limits/limits.c | 4 +-- usr.bin/login/login.c | 4 +-- usr.bin/su/su.c | 5 ++-- usr.sbin/pstat/pstat.c | 6 ++-- usr.sbin/sade/main.c | 2 +- usr.sbin/sade/variable.c | 2 +- usr.sbin/sysinstall/main.c | 2 +- usr.sbin/sysinstall/variable.c | 2 +- 19 files changed, 75 insertions(+), 136 deletions(-) diff --git a/bin/df/df.c b/bin/df/df.c index 0350d986331b..5420da92500e 100644 --- a/bin/df/df.c +++ b/bin/df/df.c @@ -131,14 +131,14 @@ main(int argc, char *argv[]) */ if (kflag) break; - putenv(strdup("BLOCKSIZE=512")); + putenv("BLOCKSIZE=512"); hflag = 0; break; case 'c': cflag = 1; break; case 'g': - putenv(strdup("BLOCKSIZE=1g")); + putenv("BLOCKSIZE=1g"); hflag = 0; break; case 'H': @@ -152,7 +152,7 @@ main(int argc, char *argv[]) break; case 'k': kflag++; - putenv(strdup("BLOCKSIZE=1024")); + putenv("BLOCKSIZE=1024"); hflag = 0; break; case 'l': @@ -162,7 +162,7 @@ main(int argc, char *argv[]) lflag = 1; break; case 'm': - putenv(strdup("BLOCKSIZE=1m")); + putenv("BLOCKSIZE=1m"); hflag = 0; break; case 'n': diff --git a/bin/sh/var.c b/bin/sh/var.c index afb144afd636..54a0a84080c3 100644 --- a/bin/sh/var.c +++ b/bin/sh/var.c @@ -319,7 +319,7 @@ setvareq(char *s, int flags) if (vp == &vmpath || (vp == &vmail && ! mpathset())) chkmail(1); if ((vp->flags & VEXPORT) && localevar(s)) { - (void) putenv(savestr(s)); + putenv(s); (void) setlocale(LC_ALL, ""); } INTON; @@ -335,7 +335,7 @@ setvareq(char *s, int flags) INTOFF; *vpp = vp; if ((vp->flags & VEXPORT) && localevar(s)) { - (void) putenv(savestr(s)); + putenv(s); (void) setlocale(LC_ALL, ""); } INTON; @@ -596,7 +596,7 @@ exportcmd(int argc, char **argv) vp->flags |= flag; if ((vp->flags & VEXPORT) && localevar(vp->text)) { - (void) putenv(savestr(vp->text)); + putenv(vp->text); (void) setlocale(LC_ALL, ""); } goto found; @@ -776,7 +776,6 @@ unsetcmd(int argc __unused, char **argv __unused) int unsetvar(char *s) { - char *eqp, *ss; struct var **vpp; struct var *vp; @@ -789,11 +788,7 @@ unsetvar(char *s) if (*(strchr(vp->text, '=') + 1) != '\0') setvar(s, nullstr, 0); if ((vp->flags & VEXPORT) && localevar(vp->text)) { - ss = savestr(s); - if ((eqp = strchr(ss, '=')) != NULL) - *eqp = '\0'; - (void) unsetenv(ss); - ckfree(ss); + unsetenv(s); setlocale(LC_ALL, ""); } vp->flags &= ~VEXPORT; diff --git a/include/stdlib.h b/include/stdlib.h index 98cb465c8f33..98fe8b7756c6 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -161,7 +161,7 @@ void _Exit(int) __dead2; int posix_memalign(void **, size_t, size_t); /* (ADV) */ int rand_r(unsigned *); /* (TSF) */ int setenv(const char *, const char *, int); -int unsetenv(const char *); +void unsetenv(const char *); #endif /* @@ -197,7 +197,7 @@ long mrand48(void); long nrand48(unsigned short[3]); int posix_openpt(int); char *ptsname(int); -int putenv(char *); +int putenv(const char *); long random(void); char *realpath(const char *, char resolved_path[]); unsigned short diff --git a/lib/libc/stdlib/getenv.3 b/lib/libc/stdlib/getenv.3 index 9ebf30bb2ed0..3d365f16a26b 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 9ff18d8b7784..306b6a19f8be 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 56b78cffd202..a5eea5d0ddfd 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 __FBSDID("$FreeBSD$"); -#include #include #include -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 5725733b5066..202c022ebfcf 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 __FBSDID("$FreeBSD$"); -#include #include #include #include -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); } diff --git a/libexec/pppoed/pppoed.c b/libexec/pppoed/pppoed.c index dc87896e7ace..8dfebb70ff27 100644 --- a/libexec/pppoed/pppoed.c +++ b/libexec/pppoed/pppoed.c @@ -258,7 +258,7 @@ Spawn(const char *prog, const char *acname, const char *provider, struct ng_mesg *rep = (struct ng_mesg *)msgbuf; struct ngpppoe_sts *sts = (struct ngpppoe_sts *)(msgbuf + sizeof *rep); struct ngpppoe_init_data *data; - char env[18], unknown[14], sessionid[5], *path; + char env[sizeof(HISMACADDR)+18], unknown[14], sessionid[5], *path; unsigned char *macaddr; const char *msg; int ret, slen; @@ -352,11 +352,11 @@ Spawn(const char *prog, const char *acname, const char *provider, /* Put the peer's MAC address in the environment */ if (sz >= sizeof(struct ether_header)) { macaddr = ((struct ether_header *)request)->ether_shost; - snprintf(env, sizeof(env), "%x:%x:%x:%x:%x:%x", + snprintf(env, sizeof(env), "%s=%x:%x:%x:%x:%x:%x", HISMACADDR, macaddr[0], macaddr[1], macaddr[2], macaddr[3], macaddr[4], macaddr[5]); - if (setenv(HISMACADDR, env, 1) != 0) - syslog(LOG_INFO, "setenv: cannot set %s: %m", HISMACADDR); + if (putenv(env) != 0) + syslog(LOG_INFO, "putenv: cannot set %s: %m", env); } /* And send our request data to the waiting node */ diff --git a/sys/sys/param.h b/sys/sys/param.h index 93ad89d0af3f..b8d5d13dc639 100644 --- a/sys/sys/param.h +++ b/sys/sys/param.h @@ -57,7 +57,7 @@ * is created, otherwise 1. */ #undef __FreeBSD_version -#define __FreeBSD_version 700038 /* Master, propagated to newvers */ +#define __FreeBSD_version 700037 /* Master, propagated to newvers */ #ifndef LOCORE #include diff --git a/usr.bin/du/du.c b/usr.bin/du/du.c index 459a4518738a..00c1f5e728d0 100644 --- a/usr.bin/du/du.c +++ b/usr.bin/du/du.c @@ -140,16 +140,16 @@ main(int argc, char *argv[]) cflag = 1; break; case 'h': - putenv(strdup("BLOCKSIZE=512")); + putenv("BLOCKSIZE=512"); hflag = 1; break; case 'k': hflag = 0; - putenv(strdup("BLOCKSIZE=1024")); + putenv("BLOCKSIZE=1024"); break; case 'm': hflag = 0; - putenv(strdup("BLOCKSIZE=1048576")); + putenv("BLOCKSIZE=1048576"); break; case 'n': nodumpflag = 1; diff --git a/usr.bin/env/env.c b/usr.bin/env/env.c index b5866efff530..815976fe5f7a 100644 --- a/usr.bin/env/env.c +++ b/usr.bin/env/env.c @@ -64,7 +64,7 @@ static void usage(void); int main(int argc, char **argv) { - char *altpath, **ep, **parg; + char *altpath, **ep, *p, **parg; char *cleanenv[1]; int ch, want_clear; @@ -102,10 +102,10 @@ main(int argc, char **argv) if (env_verbosity) fprintf(stderr, "#env clearing environ\n"); } - for (argv += optind; *argv && strchr(*argv, '=') != NULL; ++argv) { + for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) { if (env_verbosity) fprintf(stderr, "#env setenv:\t%s\n", *argv); - (void)putenv(strdup(*argv)); + (void)setenv(*argv, ++p, 1); } if (*argv) { if (altpath) diff --git a/usr.bin/limits/limits.c b/usr.bin/limits/limits.c index 854803bb1517..e976f369e2ca 100644 --- a/usr.bin/limits/limits.c +++ b/usr.bin/limits/limits.c @@ -399,8 +399,8 @@ main(int argc, char *argv[]) login_close(lc); /* set leading environment variables, like eval(1) */ - while (*argv && strchr(*argv, '=') != NULL) - (void)putenv(strdup(*argv++)); + while (*argv && (p = strchr(*argv, '='))) + (void)setenv(*argv++, ++p, 1); /* Set limits */ for (rcswhich = 0; rcswhich < RLIM_NLIMITS; rcswhich++) { diff --git a/usr.bin/login/login.c b/usr.bin/login/login.c index 4a2abb49339d..7827ea1da7d2 100644 --- a/usr.bin/login/login.c +++ b/usr.bin/login/login.c @@ -745,8 +745,8 @@ export_pam_environment() pam_env = pam_getenvlist(pamh); if (pam_env != NULL) { for (pp = pam_env; *pp != NULL; pp++) { - if (!export(*pp)) - free(*pp); + (void)export(*pp); + free(*pp); } } } diff --git a/usr.bin/su/su.c b/usr.bin/su/su.c index 26f4a3ef9209..76f76b61e64d 100644 --- a/usr.bin/su/su.c +++ b/usr.bin/su/su.c @@ -567,9 +567,8 @@ export_pam_environment(void) for (pp = environ_pam; *pp != NULL; pp++) { if (ok_to_export(*pp)) - (void)putenv(*pp); - else - free(*pp); + putenv(*pp); + free(*pp); } } diff --git a/usr.sbin/pstat/pstat.c b/usr.sbin/pstat/pstat.c index 8fdd83da7994..95f782c4e0cd 100644 --- a/usr.sbin/pstat/pstat.c +++ b/usr.sbin/pstat/pstat.c @@ -135,16 +135,16 @@ main(int argc, char *argv[]) fileflag = 1; break; case 'g': - putenv(strdup("BLOCKSIZE=1G")); + putenv("BLOCKSIZE=1G"); break; case 'h': humanflag = 1; break; case 'k': - putenv(strdup("BLOCKSIZE=1K")); + putenv("BLOCKSIZE=1K"); break; case 'm': - putenv(strdup("BLOCKSIZE=1M")); + putenv("BLOCKSIZE=1M"); break; case 'M': memf = optarg; diff --git a/usr.sbin/sade/main.c b/usr.sbin/sade/main.c index 65e14a9fd799..45ed1bbe4f9a 100644 --- a/usr.sbin/sade/main.c +++ b/usr.sbin/sade/main.c @@ -56,7 +56,7 @@ main(int argc, char **argv) /* XXX */ char *p = getenv("TERM"); if (p && strcmp(p, "cons25") == 0) - (void)putenv(strdup("TERM=cons25w")); + putenv("TERM=cons25w"); } #endif diff --git a/usr.sbin/sade/variable.c b/usr.sbin/sade/variable.c index f09819e670df..bbbc2d29d826 100644 --- a/usr.sbin/sade/variable.c +++ b/usr.sbin/sade/variable.c @@ -307,7 +307,7 @@ pvariable_set(char *var) if (index(var, '=') == NULL) msgFatal("Invalid variable format: %s", var); strlcat(tmp, var, 1024); - (void)putenv(strdup(tmp)); + putenv(tmp); } char * diff --git a/usr.sbin/sysinstall/main.c b/usr.sbin/sysinstall/main.c index c1b4e34cd937..7af8d44b61d5 100644 --- a/usr.sbin/sysinstall/main.c +++ b/usr.sbin/sysinstall/main.c @@ -74,7 +74,7 @@ main(int argc, char **argv) /* XXX */ char *p = getenv("TERM"); if (p && strcmp(p, "cons25") == 0) - (void)putenv(strdup("TERM=cons25w")); + putenv("TERM=cons25w"); } #endif diff --git a/usr.sbin/sysinstall/variable.c b/usr.sbin/sysinstall/variable.c index ad783fc63160..e86104fd100a 100644 --- a/usr.sbin/sysinstall/variable.c +++ b/usr.sbin/sysinstall/variable.c @@ -312,7 +312,7 @@ pvariable_set(char *var) if (index(var, '=') == NULL) msgFatal("Invalid variable format: %s", var); strlcat(tmp, var, 1024); - (void)putenv(strdup(tmp)); + putenv(tmp); } char *