mirror of
git://git.suckless.org/sbase
synced 2024-12-23 15:34:38 +00:00
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:
parent
b4adb4bb87
commit
1797df01e7
32
uniq.c
32
uniq.c
@ -96,7 +96,9 @@ usage(void)
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fp, *ofp;
|
||||
FILE *fp[2] = { stdin, stdout };
|
||||
int i;
|
||||
char *fname[2] = { "<stdin>", "<stdout>" };
|
||||
|
||||
ARGBEGIN {
|
||||
case 'c':
|
||||
@ -121,26 +123,18 @@ main(int argc, char *argv[])
|
||||
if (argc > 2)
|
||||
usage();
|
||||
|
||||
if (!argc) {
|
||||
uniq(stdin, stdout);
|
||||
} else {
|
||||
if (argv[0][0] == '-' && !argv[0][1]) {
|
||||
argv[0] = "<stdin>";
|
||||
fp = stdin;
|
||||
} else if (!(fp = fopen(argv[0], "r"))) {
|
||||
eprintf("fopen %s:", argv[0]);
|
||||
for (i = 0; i < argc; i++) {
|
||||
if (strcmp(argv[i], "-")) {
|
||||
fname[i] = argv[i];
|
||||
if (!(fp[i] = fopen(argv[i], (i == 0) ? "r" : "w")))
|
||||
eprintf("fopen %s:", argv[i]);
|
||||
}
|
||||
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]);
|
||||
efshut(ofp, argv[1]);
|
||||
uniq(fp[0], fp[1]);
|
||||
uniqfinish(fp[1]);
|
||||
|
||||
efshut(fp[0], fname[0]);
|
||||
efshut(fp[1], fname[1]);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user