[hackers] [sbase] Fix uniq(1) || FRIGN

From: <git_AT_suckless.org>
Date: Tue, 19 May 2015 17:51:23 +0200 (CEST)

commit 1797df01e7c6354b82f2b924c9a461a97331f08c
Author: FRIGN <dev_AT_frign.de>
Date: Tue May 19 15:30:09 2015 +0200

    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.

diff --git a/uniq.c b/uniq.c
index 922ba39..1b30c1a 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -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':
_AT_@ -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;
 }
Received on Tue May 19 2015 - 17:51:23 CEST

This archive was generated by hypermail 2.3.0 : Tue May 19 2015 - 18:00:11 CEST