Fix uniq(1)

The argument handling was quite garbled up. So I fixed it.
In the process, it drops a lot of locs.

Previously, it would lead to an off-by-one in an edge case, so
stop messing around with argv and use an idiomatic fp- and fname-
array.

Now this works fine and is much easier to read.

This is also the first step towards going back to strcmp() instead
of handrolling the "-"-checks.
This commit is contained in:
FRIGN 2015-05-19 15:30:09 +02:00 committed by sin
parent b4adb4bb87
commit 1797df01e7

32
uniq.c
View File

@ -96,7 +96,9 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp, *ofp; FILE *fp[2] = { stdin, stdout };
int i;
char *fname[2] = { "<stdin>", "<stdout>" };
ARGBEGIN { ARGBEGIN {
case 'c': case 'c':
@ -121,26 +123,18 @@ main(int argc, char *argv[])
if (argc > 2) if (argc > 2)
usage(); usage();
if (!argc) { for (i = 0; i < argc; i++) {
uniq(stdin, stdout); if (strcmp(argv[i], "-")) {
} else { fname[i] = argv[i];
if (argv[0][0] == '-' && !argv[0][1]) { if (!(fp[i] = fopen(argv[i], (i == 0) ? "r" : "w")))
argv[0] = "<stdin>"; eprintf("fopen %s:", argv[i]);
fp = stdin;
} else if (!(fp = fopen(argv[0], "r"))) {
eprintf("fopen %s:", argv[0]);
} }
if (argc == 1 || (argv[1][0] == '-' && !argv[1][1])) {
argv[1] = "<stdout>";
ofp = stdout;
} else if (!(ofp = fopen(argv[1], "w"))) {
eprintf("fopen %s:", argv[1]);
} }
uniq(fp, ofp);
}
uniqfinish(ofp);
efshut(fp, argv[0]); uniq(fp[0], fp[1]);
efshut(ofp, argv[1]); uniqfinish(fp[1]);
efshut(fp[0], fname[0]);
efshut(fp[1], fname[1]);
return 0; return 0;
} }