uniq: add support for writing output files

This commit is contained in:
Tai Chi Minh Ralph Eastwood 2015-02-11 14:56:16 +00:00 committed by sin
parent f9846a9a6b
commit 70694a318c
2 changed files with 29 additions and 19 deletions

2
uniq.1
View File

@ -10,7 +10,7 @@
.Op Fl d | u .Op Fl d | u
.Op Fl f Ar fields .Op Fl f Ar fields
.Op Fl s Ar chars .Op Fl s Ar chars
.Op Ar file .Op Ar input Op Ar output
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
reads reads

44
uniq.c
View File

@ -8,9 +8,9 @@
#include "text.h" #include "text.h"
#include "util.h" #include "util.h"
static void uniqline(char *); static void uniqline(FILE *, char *);
static void uniq(FILE *, const char *); static void uniq(FILE *, FILE *);
static void uniqfinish(void); static void uniqfinish(FILE *);
static const char *countfmt = ""; static const char *countfmt = "";
static int dflag = 0; static int dflag = 0;
@ -25,13 +25,14 @@ static long prevlinecount = 0;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-c] [-d | -u] [-f fields] [-s chars] [file]\n", argv0); eprintf("usage: %s [-c] [-d | -u] [-f fields] [-s chars]"
" [[input] output]\n", argv0);
} }
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp; FILE *fp = stdin, *ofp = stdout;
ARGBEGIN { ARGBEGIN {
case 'c': case 'c':
@ -54,15 +55,24 @@ main(int argc, char *argv[])
} ARGEND; } ARGEND;
if (argc == 0) { if (argc == 0) {
uniq(stdin, "<stdin>"); uniq(stdin, stdout);
} else if (argc == 1) { } else if (argc >= 1) {
if (!(fp = fopen(argv[0], "r"))) if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
uniq(fp, argv[0]); if (argc == 2) {
if (strcmp(argv[1], "-") &&
!(ofp = fopen(argv[1], "w")))
eprintf("fopen %s:", argv[1]);
} else
eprintf("extra argument: %s\n", argv[2]);
uniq(fp, ofp);
if (fp != stdin)
fclose(fp); fclose(fp);
} else } else
usage(); usage();
uniqfinish(); uniqfinish(ofp);
if (ofp != stdout)
fclose(ofp);
return 0; return 0;
} }
@ -84,7 +94,7 @@ uniqskip(char *l)
} }
static void static void
uniqline(char *l) uniqline(FILE *ofp, char *l)
{ {
char *loffset = l ? uniqskip(l) : l; char *loffset = l ? uniqskip(l) : l;
@ -100,8 +110,8 @@ uniqline(char *l)
if (prevline) { if (prevline) {
if ((prevlinecount == 1 && !dflag) || if ((prevlinecount == 1 && !dflag) ||
(prevlinecount != 1 && !uflag)) { (prevlinecount != 1 && !uflag)) {
printf(countfmt, prevlinecount); fprintf(ofp, countfmt, prevlinecount);
fputs(prevline, stdout); fputs(prevline, ofp);
} }
free(prevline); free(prevline);
prevline = prevoffset = NULL; prevline = prevoffset = NULL;
@ -115,17 +125,17 @@ uniqline(char *l)
} }
static void static void
uniq(FILE *fp, const char *str) uniq(FILE *fp, FILE *ofp)
{ {
char *buf = NULL; char *buf = NULL;
size_t size = 0; size_t size = 0;
while (getline(&buf, &size, fp) != -1) while (getline(&buf, &size, fp) != -1)
uniqline(buf); uniqline(ofp, buf);
} }
static void static void
uniqfinish(void) uniqfinish(FILE *ofp)
{ {
uniqline(NULL); uniqline(ofp, NULL);
} }