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
=*| uname yes none
#*| unexpand yes none
=* uniq yes none
=*| uniq yes none
=*| unlink yes none
=* uudecode 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
.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

15
uniq.c
View File

@ -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,23 +121,19 @@ 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();
}
uniqfinish(ofp);
if (ofp != stdout)
fclose(ofp);
return 0;