1
0
mirror of git://git.suckless.org/sbase synced 2024-12-22 23:13:44 +00:00
sbase/printenv.c
FRIGN 286df29e7d Make already audited tools argv-centric instead of argc-centric
This has already been suggested by Evan Gates <evan.gates@gmail.com>
and he's totally right about it.
So, what's the problem?
I wrote a testing program asshole.c with

int
main(void)
{
        execl("/path/to/sbase/echo", "echo", "test");
        return 0;
}

and checked the results with glibc and musl. Note that the
sentinel NULL is missing from the end of the argument list.
glibc calculates an argc of 5, musl 4 (instead of 2) and thus
mess up things anyway.
The powerful arg.h also focuses on argv instead of argc as well,
but ignoring argc completely is also the wrong way to go.
Instead, a more idiomatic approach is to check *argv only and
decrement argc on the go.
While at it, I rewrote yes(1) in an argv-centric way as well.

All audited tools have been "fixed" and each following audited
tool will receive the same treatment.
2015-03-02 14:19:26 +01:00

40 lines
515 B
C

/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
extern char **environ;
static void
usage(void)
{
eprintf("usage: %s [var ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
char *var;
int ret = 0;
ARGBEGIN {
default:
usage();
} ARGEND;
if (!argc) {
for (; *environ; environ++)
puts(*environ);
} else {
for (; *argv; argc--, argv++) {
if ((var = getenv(*argv)))
puts(var);
else
ret = 1;
}
}
return ret;
}