svn commit: r318316 - head/usr.bin/uniq

Ed Maste emaste at FreeBSD.org
Mon May 15 20:18:15 UTC 2017


Author: emaste
Date: Mon May 15 20:18:14 2017
New Revision: 318316
URL: https://svnweb.freebsd.org/changeset/base/318316

Log:
  uniq: allow -c to be used with -d or -u
  
  Bring in some bits from NetBSD and lift the restriction in uniq(1) that
  -c cannot be used with the -d and -u options.  This restriction seems
  unnecessary and is supported at least by GNU, OpenBSD, and NetBSD.  Lift
  the restriction and simplify the show() logic a little bit to maintain
  functionality when -c is provided with -d/-u.
  
  Also with this change, -d and -u are now actually a mutually exclusive,
  albeit valid, combination.  Given that they both indicate opposite
  behavior, uniq(1) will no longer output anything if both -d and -u are
  supplied.  This is in line with NetBSD as well as GNU.
  
  Adjust the man page and usage() to reflect that -c is its own standalone
  option.
  
  PR:		200553
  Submitted by:	Kyle Evans <kevans91 at ksu.edu>
  Reviewed by:	cem, emaste
  MFC after:	2 weeks
  Differential Revision:	https://reviews.freebsd.org/D10694

Modified:
  head/usr.bin/uniq/uniq.1
  head/usr.bin/uniq/uniq.c

Modified: head/usr.bin/uniq/uniq.1
==============================================================================
--- head/usr.bin/uniq/uniq.1	Mon May 15 19:58:01 2017	(r318315)
+++ head/usr.bin/uniq/uniq.1	Mon May 15 20:18:14 2017	(r318316)
@@ -31,7 +31,7 @@
 .\"     From: @(#)uniq.1	8.1 (Berkeley) 6/6/93
 .\" $FreeBSD$
 .\"
-.Dd December 17, 2009
+.Dd May 15, 2017
 .Dt UNIQ 1
 .Os
 .Sh NAME
@@ -39,7 +39,8 @@
 .Nd report or filter out repeated lines in a file
 .Sh SYNOPSIS
 .Nm
-.Op Fl c | Fl d | Fl u
+.Op Fl c
+.Op Fl d | Fl u
 .Op Fl i
 .Op Fl f Ar num
 .Op Fl s Ar chars

Modified: head/usr.bin/uniq/uniq.c
==============================================================================
--- head/usr.bin/uniq/uniq.c	Mon May 15 19:58:01 2017	(r318315)
+++ head/usr.bin/uniq/uniq.c	Mon May 15 20:18:14 2017	(r318316)
@@ -129,13 +129,6 @@ main (int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
-	/* If no flags are set, default is -d -u. */
-	if (cflag) {
-		if (dflag || uflag)
-			usage();
-	} else if (!dflag && !uflag)
-		dflag = uflag = 1;
-
 	if (argc > 2)
 		usage();
 
@@ -182,9 +175,6 @@ main (int argc, char *argv[])
 	}
 	tprev = convert(prevline);
 
-	if (!cflag && uflag && dflag)
-		show(ofp, prevline);
-
 	tthis = NULL;
 	while (getline(&thisline, &thisbuflen, ifp) >= 0) {
 		if (tthis != NULL)
@@ -200,8 +190,7 @@ main (int argc, char *argv[])
 
 		if (comp) {
 			/* If different, print; set previous to new value. */
-			if (cflag || !dflag || !uflag)
-				show(ofp, prevline);
+			show(ofp, prevline);
 			p = prevline;
 			b1 = prevbuflen;
 			prevline = thisline;
@@ -209,8 +198,6 @@ main (int argc, char *argv[])
 			if (tprev != NULL)
 				free(tprev);
 			tprev = tthis;
-			if (!cflag && uflag && dflag)
-				show(ofp, prevline);
 			thisline = p;
 			thisbuflen = b1;
 			tthis = NULL;
@@ -220,8 +207,7 @@ main (int argc, char *argv[])
 	}
 	if (ferror(ifp))
 		err(1, "%s", ifn);
-	if (cflag || !dflag || !uflag)
-		show(ofp, prevline);
+	show(ofp, prevline);
 	exit(0);
 }
 
@@ -286,9 +272,11 @@ static void
 show(FILE *ofp, const char *str)
 {
 
+	if ((dflag && repeats == 0) || (uflag && repeats > 0))
+		return;
 	if (cflag)
 		(void)fprintf(ofp, "%4d %s", repeats + 1, str);
-	if ((dflag && repeats) || (uflag && !repeats))
+	else
 		(void)fprintf(ofp, "%s", str);
 }
 
@@ -351,6 +339,6 @@ static void
 usage(void)
 {
 	(void)fprintf(stderr,
-"usage: uniq [-c | -d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
+"usage: uniq [-c] [-d | -u] [-i] [-f fields] [-s chars] [input [output]]\n");
 	exit(1);
 }


More information about the svn-src-all mailing list