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

From: <git_AT_suckless.org>
Date: Thu, 19 Mar 2015 11:13:24 +0100 (CET)

commit 45db281d7c5e983ed814f45161f4d3e6c6791ee1
Author: FRIGN <dev_AT_frign.de>
Date: Tue Mar 17 23:59:09 2015 +0100

    Audit uniq(1)
    
    Refactor the manpage and small style-changes in uniq.c.
    Remove unnecessary "else", we catch argc > 2 earlier already.

diff --git a/README b/README
index 92f76f7..755d428 100644
--- a/README
+++ b/README
_AT_@ -82,7 +82,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
 =*| tty yes none
 =*| uname yes none
 #*| unexpand yes none
-=* uniq yes none
+=*| uniq yes none
 =*| unlink yes none
 =* uudecode yes none
 =* uuencode yes none
diff --git a/uniq.1 b/uniq.1
index c32efe4..fcee101 100644
--- a/uniq.1
+++ b/uniq.1
_AT_@ -1,4 +1,4 @@
-.Dd January 30, 2015
+.Dd March 17, 2015
 .Dt UNIQ 1
 .Os sbase
 .Sh NAME
_AT_@ -8,8 +8,8 @@
 .Nm
 .Op Fl c
 .Op Fl d | u
-.Op Fl f Ar fields
-.Op Fl s Ar chars
+.Op Fl f Ar num
+.Op Fl s Ar num
 .Op Ar input Op Ar output
 .Sh DESCRIPTION
 .Nm
_AT_@ -20,29 +20,24 @@ duplicate lines to the
 .Ar output
 file. If no
 .Ar input
-file is given,
+file is given
 .Nm
 reads from stdin. If no
 .Ar output
-file is given, then
+file is given
 .Nm
 writes to stdout.
 .Sh OPTIONS
 .Bl -tag -width Ds
 .It Fl c
-Prefixes each line with a count of its consecutive occurrences in the input.
-.It Fl d
-Suppresses non-duplicate lines (thus 'uniq -d' prints only duplicates).
-.It Fl u
-Suppresses non-unique lines (thus 'uniq -u' prints only uniques).
-.It Fl f Ar fields
+Prefix each line with the number of consecutive occurrences in
+.Ar input .
+.It Fl d | Fl u
+Print duplicate | unique lines only.
+.It Fl f Ar num | Fl s Ar num
 Ignore the first
-.Ar fields
-in each input line when doing comparisons.
-.It Fl s Ar chars
-Ignore the first
-.Ar chars
-characters in each input line when doing comparisons.
+.Ar num
+fields | characters in each input line when doing comparisons.
 .El
 .Sh STANDARDS
 The
diff --git a/uniq.c b/uniq.c
index b6ac93f..6d1e139 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -12,8 +12,8 @@ static int uflag = 0;
 static int fskip = 0;
 static int sskip = 0;
 
-static char *prevline = NULL;
-static char *prevoffset = NULL;
+static char *prevline = NULL;
+static char *prevoffset = NULL;
 static long prevlinecount = 0;
 static size_t prevlinesiz = 0;
 
_AT_@ -30,6 +30,7 @@ uniqskip(const char *l)
                         lo++;
         }
         for (; s && *lo && *lo != '\n'; --s, ++lo);
+
         return lo;
 }
 
_AT_@ -75,7 +76,7 @@ uniq(FILE *fp, FILE *ofp)
         size_t size = 0;
         ssize_t len;
 
- while ((len = getline(&buf, &size, fp)) != -1)
+ while ((len = getline(&buf, &size, fp)) >= 0)
                 uniqline(ofp, buf, (size_t)len);
 }
 
_AT_@ -120,24 +121,20 @@ main(int argc, char *argv[])
         if (argc > 2)
                 usage();
 
- if (argc == 0) {
+ if (!argc) {
                 uniq(stdin, stdout);
- } else if (argc >= 1) {
+ } else {
                 if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r")))
                         eprintf("fopen %s:", argv[0]);
                 if (argc == 2) {
- if (strcmp(argv[1], "-") &&
- !(ofp = fopen(argv[1], "w")))
+ if (strcmp(argv[1], "-") && !(ofp = fopen(argv[1], "w")))
                                 eprintf("fopen %s:", argv[1]);
                 }
                 uniq(fp, ofp);
- if (fp != stdin)
- fclose(fp);
- } else
- usage();
+ fclose(fp);
+ }
         uniqfinish(ofp);
- if (ofp != stdout)
- fclose(ofp);
+ fclose(ofp);
 
         return 0;
 }
Received on Thu Mar 19 2015 - 11:13:24 CET

This archive was generated by hypermail 2.3.0 : Thu Mar 19 2015 - 11:24:36 CET