Audit uniq(1)

Refactor the manpage and small style-changes in uniq.c.
Remove unnecessary "else", we catch argc > 2 earlier already.
This commit is contained in:
FRIGN 2015-03-17 23:59:09 +01:00
parent 5af4cdcd60
commit 4af8889396
3 changed files with 23 additions and 31 deletions

2
README
View File

@ -82,7 +82,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=*| tty yes none =*| tty yes none
=*| uname yes none =*| uname yes none
#*| unexpand yes none #*| unexpand yes none
=* uniq yes none =*| uniq yes none
=*| unlink yes none =*| unlink yes none
=* uudecode yes none =* uudecode yes none
=* uuencode yes none =* uuencode yes none

29
uniq.1
View File

@ -1,4 +1,4 @@
.Dd January 30, 2015 .Dd March 17, 2015
.Dt UNIQ 1 .Dt UNIQ 1
.Os sbase .Os sbase
.Sh NAME .Sh NAME
@ -8,8 +8,8 @@
.Nm .Nm
.Op Fl c .Op Fl c
.Op Fl d | u .Op Fl d | u
.Op Fl f Ar fields .Op Fl f Ar num
.Op Fl s Ar chars .Op Fl s Ar num
.Op Ar input Op Ar output .Op Ar input Op Ar output
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
@ -20,29 +20,24 @@ duplicate lines to the
.Ar output .Ar output
file. If no file. If no
.Ar input .Ar input
file is given, file is given
.Nm .Nm
reads from stdin. If no reads from stdin. If no
.Ar output .Ar output
file is given, then file is given
.Nm .Nm
writes to stdout. writes to stdout.
.Sh OPTIONS .Sh OPTIONS
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl c .It Fl c
Prefixes each line with a count of its consecutive occurrences in the input. Prefix each line with the number of consecutive occurrences in
.It Fl d .Ar input .
Suppresses non-duplicate lines (thus 'uniq -d' prints only duplicates). .It Fl d | Fl u
.It Fl u Print duplicate | unique lines only.
Suppresses non-unique lines (thus 'uniq -u' prints only uniques). .It Fl f Ar num | Fl s Ar num
.It Fl f Ar fields
Ignore the first Ignore the first
.Ar fields .Ar num
in each input line when doing comparisons. fields | characters 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.
.El .El
.Sh STANDARDS .Sh STANDARDS
The The

23
uniq.c
View File

@ -12,8 +12,8 @@ static int uflag = 0;
static int fskip = 0; static int fskip = 0;
static int sskip = 0; static int sskip = 0;
static char *prevline = NULL; static char *prevline = NULL;
static char *prevoffset = NULL; static char *prevoffset = NULL;
static long prevlinecount = 0; static long prevlinecount = 0;
static size_t prevlinesiz = 0; static size_t prevlinesiz = 0;
@ -30,6 +30,7 @@ uniqskip(const char *l)
lo++; lo++;
} }
for (; s && *lo && *lo != '\n'; --s, ++lo); for (; s && *lo && *lo != '\n'; --s, ++lo);
return lo; return lo;
} }
@ -75,7 +76,7 @@ uniq(FILE *fp, FILE *ofp)
size_t size = 0; size_t size = 0;
ssize_t len; ssize_t len;
while ((len = getline(&buf, &size, fp)) != -1) while ((len = getline(&buf, &size, fp)) >= 0)
uniqline(ofp, buf, (size_t)len); uniqline(ofp, buf, (size_t)len);
} }
@ -120,24 +121,20 @@ main(int argc, char *argv[])
if (argc > 2) if (argc > 2)
usage(); usage();
if (argc == 0) { if (!argc) {
uniq(stdin, stdout); uniq(stdin, stdout);
} else if (argc >= 1) { } else {
if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r"))) if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
if (argc == 2) { if (argc == 2) {
if (strcmp(argv[1], "-") && if (strcmp(argv[1], "-") && !(ofp = fopen(argv[1], "w")))
!(ofp = fopen(argv[1], "w")))
eprintf("fopen %s:", argv[1]); eprintf("fopen %s:", argv[1]);
} }
uniq(fp, ofp); uniq(fp, ofp);
if (fp != stdin) fclose(fp);
fclose(fp); }
} else
usage();
uniqfinish(ofp); uniqfinish(ofp);
if (ofp != stdout) fclose(ofp);
fclose(ofp);
return 0; return 0;
} }