Audit printenv(1)

1) argc style
2) safeguard argv-loop as already seen in echo(1) with argc-decrement.
This commit is contained in:
FRIGN 2015-02-28 21:47:17 +01:00
parent 6f715d8c6d
commit 0226c05105
3 changed files with 7 additions and 6 deletions

2
README
View File

@ -51,7 +51,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
= nl no -d, -f, -h, -l, -p = nl no -d, -f, -h, -l, -p
=* nohup yes none =* nohup yes none
#* paste yes none #* paste yes none
=* printenv non-posix none =*| printenv non-posix none
#* printf yes none #* printf yes none
=* pwd yes none =* pwd yes none
= readlink non-posix none = readlink non-posix none

2
echo.c
View File

@ -23,7 +23,7 @@ main(int argc, char *argv[])
} ARGEND; } ARGEND;
for (; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
putword(argv[0]); putword(*argv);
if (!nflag) if (!nflag)
putchar('\n'); putchar('\n');

View File

@ -9,7 +9,7 @@ extern char **environ;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [variable...]\n", argv0); eprintf("usage: %s [var ...]\n", argv0);
} }
int int
@ -23,16 +23,17 @@ main(int argc, char *argv[])
usage(); usage();
} ARGEND; } ARGEND;
if (argc == 0) { if (!argc) {
while (*environ) while (*environ)
printf("%s\n", *environ++); printf("%s\n", *environ++);
} else { } else {
while (*argv) { for (; argc > 0; argc--, argv++) {
if ((var = getenv(*argv++))) if ((var = getenv(*argv)))
printf("%s\n", var); printf("%s\n", var);
else else
ret = 1; ret = 1;
} }
} }
return ret; return ret;
} }