Audit env(1)

1) Shorten synopsis and reflect this in the manual
2) Use argv0 in usage()
3) Decrement argc in argv-loop for consistency
4) Make it clearer which error-code results from which errno in enprintf
5) Use idiomatic for-loop also for environ. Don't increment these pointers
   in the loop itself!
This commit is contained in:
FRIGN 2015-03-02 14:50:43 +01:00
parent 933ed8c00b
commit 27656a0cbc
3 changed files with 14 additions and 14 deletions

2
README
View File

@ -27,7 +27,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=*| dirname yes none =*| dirname yes none
=* du yes none =* du yes none
=*| echo yes none =*| echo yes none
=* env yes none =*| env yes none
#* expand yes none #* expand yes none
#* expr yes none #* expr yes none
=*| false yes none =*| false yes none

16
env.1
View File

@ -1,4 +1,4 @@
.Dd January 24, 2015 .Dd March 1, 2015
.Dt ENV 1 .Dt ENV 1
.Os sbase .Os sbase
.Sh NAME .Sh NAME
@ -7,15 +7,15 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl i .Op Fl i
.Oo Fl u Ar variable Oc ... .Oo Fl u Ar var Oc ...
.Oo Ar variable Ns = Ns Ar value Oc ... .Oo Ar var Ns = Ns Ar value Oc ...
.Oo Ar cmd Oo arg ... Oc Oc .Oo Ar cmd Oo arg ... Oc Oc
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
unsets each unsets each
.Ar variable , .Ar var ,
then adds or sets each then adds or sets each
.Ar ( variable , value ) .Ar ( var , value )
tuple in the environment. tuple in the environment.
.Pp .Pp
If If
@ -28,11 +28,11 @@ otherwise, the modified environment is printed to stdout.
Completely ignore the existing environment and execute Completely ignore the existing environment and execute
.Ar cmd .Ar cmd
only with each only with each
.Ar ( variable , value ) .Ar ( var , value )
tuple specified. tuple specified.
.It Fl u Ar variable .It Fl u Ar var
Unset Unset
.Ar variable .Ar var
in the environment. in the environment.
.El .El
.Sh SEE ALSO .Sh SEE ALSO

10
env.c
View File

@ -12,7 +12,7 @@ extern char **environ;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: env [-i] [-u variable] ... [variable=value] ... [cmd [arg ...]]\n"); eprintf("usage: %s [-i] [-u var] ... [var=value] ... [cmd [arg ...]]\n", argv0);
} }
int int
@ -30,16 +30,16 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
for (; *argv && strchr(*argv, '='); argv++) for (; *argv && strchr(*argv, '='); argc--, argv++)
putenv(*argv); putenv(*argv);
if (*argv) { if (*argv) {
execvp(*argv, argv); execvp(*argv, argv);
enprintf(127 - (errno != EEXIST), "env: '%s':", *argv); enprintf(126 + (errno == EEXIST), "execvp: %s:", *argv);
} }
while (environ && *environ) for (; environ && *environ; environ++)
printf("%s\n", *environ++); puts(*environ);
return 0; return 0;
} }