Attached to probably complete uniq functionality to POSIX-2008?
From 0025ad0d56cae46275bfec1d9fe61f82e6ec5eb5 Mon Sep 17 00:00:00 2001
From: Tai Chi Minh Ralph Eastwood <[email protected]>
Date: Wed, 11 Feb 2015 15:18:10 +0000
Subject: [PATCH 2/2] uniq.1: add [input [output]] information

---
 uniq.1 | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/uniq.1 b/uniq.1
index 762601e..720ffe9 100644
--- a/uniq.1
+++ b/uniq.1
@@ -13,14 +13,20 @@
 .Op Ar input Op Ar output
 .Sh DESCRIPTION
 .Nm
-reads
-.Ar file
-and writes one copy of a line from each group of consecutive
-duplicate lines to stdout. If no
-.Ar file
-is given,
+reads the
+.Ar input
+file and writes one copy of a line from each group of consecutive
+duplicate lines to the
+.Ar output
+file. If no
+.Ar input
+file is given,
 .Nm
-reads from stdin.
+reads from stdin. If no
+.Ar output
+file is given, then
+.Nm
+writes to stdout.
 .Sh OPTIONS
 .Bl -tag -width Ds
 .It Fl c
-- 
2.3.0

From 09094f93f1f7f4122a8f7c4be9e770aaccfc8ebb Mon Sep 17 00:00:00 2001
From: Tai Chi Minh Ralph Eastwood <[email protected]>
Date: Wed, 11 Feb 2015 14:56:16 +0000
Subject: [PATCH 1/2] uniq: add support for writing output files

---
 uniq.1 |  2 +-
 uniq.c | 46 ++++++++++++++++++++++++++++------------------
 2 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/uniq.1 b/uniq.1
index 08d190b..762601e 100644
--- a/uniq.1
+++ b/uniq.1
@@ -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
@@ -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;
@@ -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':
@@ -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;
 }
@@ -84,7 +94,7 @@ uniqskip(char *l)
 }
 
 static void
-uniqline(char *l)
+uniqline(FILE *ofp, char *l)
 {
 	char *loffset = l ? uniqskip(l) : l;
 
@@ -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;
@@ -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);
 }
-- 
2.3.0

Reply via email to