From 4af8889396924ab79f75c6042886780e06102360 Mon Sep 17 00:00:00 2001 From: FRIGN Date: Tue, 17 Mar 2015 23:59:09 +0100 Subject: [PATCH] Audit uniq(1) Refactor the manpage and small style-changes in uniq.c. Remove unnecessary "else", we catch argc > 2 earlier already. --- README | 2 +- uniq.1 | 29 ++++++++++++----------------- uniq.c | 23 ++++++++++------------- 3 files changed, 23 insertions(+), 31 deletions(-) diff --git a/README b/README index 92f76f7..755d428 100644 --- a/README +++ b/README @@ -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 @@ -1,4 +1,4 @@ -.Dd January 30, 2015 +.Dd March 17, 2015 .Dt UNIQ 1 .Os sbase .Sh NAME @@ -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 @@ -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 @@ -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; @@ -30,6 +30,7 @@ uniqskip(const char *l) lo++; } for (; s && *lo && *lo != '\n'; --s, ++lo); + return lo; } @@ -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); } @@ -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; }