[hackers] [sbase] uniq: add support for writing output files || Tai Chi Minh Ralph Eastwood

From: <git_AT_suckless.org>
Date: Wed, 11 Feb 2015 16:30:50 +0100 (CET)

commit c15103a0c8440f5c645b435bf2c2c42e605290b1
Author: Tai Chi Minh Ralph Eastwood <tcmreastwood_AT_gmail.com>
Date: Wed Feb 11 14:56:16 2015 +0000

    uniq: add support for writing output files

diff --git a/uniq.1 b/uniq.1
index 08d190b..762601e 100644
--- a/uniq.1
+++ b/uniq.1
_AT_@ -10,7 +10,7 @@
 .Op Fl d | u
 .Op Fl f Ar fields
 .Op Fl s Ar chars
-.Op Ar file
+.Op Ar input Op Ar output
 .Sh DESCRIPTION
 .Nm
 reads
diff --git a/uniq.c b/uniq.c
index c01a758..1e7d09c 100644
--- a/uniq.c
+++ b/uniq.c
_AT_@ -8,9 +8,9 @@
 #include "text.h"
 #include "util.h"
 
-static void uniqline(char *);
-static void uniq(FILE *, const char *);
-static void uniqfinish(void);
+static void uniqline(FILE *, char *);
+static void uniq(FILE *, FILE *);
+static void uniqfinish(FILE *);
 
 static const char *countfmt = "";
 static int dflag = 0;
_AT_@ -25,13 +25,14 @@ static long prevlinecount = 0;
 static 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
 main(int argc, char *argv[])
 {
- FILE *fp;
+ FILE *fp = stdin, *ofp = stdout;
 
         ARGBEGIN {
         case 'c':
_AT_@ -54,15 +55,24 @@ main(int argc, char *argv[])
         } ARGEND;
 
         if (argc == 0) {
- uniq(stdin, "<stdin>");
- } else if (argc == 1) {
- if (!(fp = fopen(argv[0], "r")))
+ uniq(stdin, stdout);
+ } else if (argc >= 1) {
+ if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r")))
                         eprintf("fopen %s:", argv[0]);
- uniq(fp, argv[0]);
- fclose(fp);
+ 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);
         } else
                 usage();
- uniqfinish();
+ uniqfinish(ofp);
+ if (ofp != stdout)
+ fclose(ofp);
 
         return 0;
 }
_AT_@ -84,7 +94,7 @@ uniqskip(char *l)
 }
 
 static void
-uniqline(char *l)
+uniqline(FILE *ofp, char *l)
 {
         char *loffset = l ? uniqskip(l) : l;
 
_AT_@ -100,8 +110,8 @@ uniqline(char *l)
         if (prevline) {
                 if ((prevlinecount == 1 && !dflag) ||
                     (prevlinecount != 1 && !uflag)) {
- printf(countfmt, prevlinecount);
- fputs(prevline, stdout);
+ fprintf(ofp, countfmt, prevlinecount);
+ fputs(prevline, ofp);
                 }
                 free(prevline);
                 prevline = prevoffset = NULL;
_AT_@ -115,17 +125,17 @@ uniqline(char *l)
 }
 
 static void
-uniq(FILE *fp, const char *str)
+uniq(FILE *fp, FILE *ofp)
 {
         char *buf = NULL;
         size_t size = 0;
 
         while (getline(&buf, &size, fp) != -1)
- uniqline(buf);
+ uniqline(ofp, buf);
 }
 
 static void
-uniqfinish(void)
+uniqfinish(FILE *ofp)
 {
- uniqline(NULL);
+ uniqline(ofp, NULL);
 }
Received on Wed Feb 11 2015 - 16:30:50 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 11 2015 - 16:36:09 CET