Skip to content

Commit

Permalink
Back out all POSIXified *env() changes.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ache authored and ache committed May 1, 2007
1 parent 61e9800 commit 6ccaf05
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 136 deletions.
8 changes: 4 additions & 4 deletions bin/df/df.c
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand All @@ -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':
Expand All @@ -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':
Expand Down
13 changes: 4 additions & 9 deletions bin/sh/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -776,7 +776,6 @@ unsetcmd(int argc __unused, char **argv __unused)
int
unsetvar(char *s)
{
char *eqp, *ss;
struct var **vpp;
struct var *vp;

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/*
Expand Down Expand Up @@ -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
Expand Down
53 changes: 18 additions & 35 deletions lib/libc/stdlib/getenv.3
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 2 additions & 5 deletions lib/libc/stdlib/getenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -85,8 +85,5 @@ getenv(name)
{
int offset;

if (name == NULL || !*name || strchr(name, '=') != NULL)
return (NULL);

return (__findenv(name, &offset));
}
48 changes: 11 additions & 37 deletions lib/libc/stdlib/putenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
29 changes: 10 additions & 19 deletions lib/libc/stdlib/setenv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);

/*
Expand All @@ -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)
Expand All @@ -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++); );
Expand All @@ -104,22 +101,16 @@ setenv(name, value, rewrite)
* unsetenv(name) --
* Delete environmental variable "name".
*/
int
void
unsetenv(name)
const char *name;
{
extern char **environ;
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);
}
8 changes: 4 additions & 4 deletions libexec/pppoed/pppoed.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down
Loading

0 comments on commit 6ccaf05

Please sign in to comment.