diff --git a/usr.bin/uniq/tests/uniq_test.sh b/usr.bin/uniq/tests/uniq_test.sh --- a/usr.bin/uniq/tests/uniq_test.sh +++ b/usr.bin/uniq/tests/uniq_test.sh @@ -122,6 +122,30 @@ atf_check_uniq --unique } +atf_test_case interactive +interactive_head() { + atf_set descr "test interactive use" +} +interactive_body() { + sh -c 'yes | stdbuf -oL uniq >actual' & + pid=$! + sleep 1 + kill $! + atf_check -o inline:"y\n" cat actual +} + +atf_test_case interactive_repeated +interactive_repeated_head() { + atf_set descr "test interactive use with -d" +} +interactive_repeated_body() { + sh -c 'yes | stdbuf -oL uniq -d >actual' & + pid=$! + sleep 1 + kill $! + atf_check -o inline:"y\n" cat actual +} + atf_init_test_cases() { atf_add_test_case basic @@ -134,4 +158,6 @@ atf_add_test_case ignore_case atf_add_test_case skip_chars atf_add_test_case unique + atf_add_test_case interactive + atf_add_test_case interactive_repeated } diff --git a/usr.bin/uniq/uniq.1 b/usr.bin/uniq/uniq.1 --- a/usr.bin/uniq/uniq.1 +++ b/usr.bin/uniq/uniq.1 @@ -28,7 +28,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 7, 2020 +.Dd January 11, 2024 .Dt UNIQ 1 .Os .Sh NAME @@ -70,8 +70,13 @@ .It Fl c , Fl -count Precede each output line with the count of the number of times the line occurred in the input, followed by a single space. +Ignored if +.Fl u +is also specified. .It Fl d , Fl -repeated Output a single copy of each line that is repeated in the input. +Ignored if +.Fl D is also specified. .It Fl D , Fl -all-repeated Op Ar septype Output all lines that are repeated (like .Fl d , diff --git a/usr.bin/uniq/uniq.c b/usr.bin/uniq/uniq.c --- a/usr.bin/uniq/uniq.c +++ b/usr.bin/uniq/uniq.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -51,14 +52,9 @@ #include #include -static int Dflag, cflag, dflag, uflag, iflag; -static int numchars, numfields, repeats; - -/* Dflag values */ -#define DF_NONE 0 -#define DF_NOSEP 1 -#define DF_PRESEP 2 -#define DF_POSTSEP 3 +static enum { DF_NONE, DF_NOSEP, DF_PRESEP, DF_POSTSEP } Dflag; +static bool cflag, dflag, uflag, iflag; +static long long numchars, numfields, repeats; static const struct option long_opts[] = { @@ -108,13 +104,13 @@ usage(); break; case 'c': - cflag = 1; + cflag = true; break; case 'd': - dflag = 1; + dflag = true; break; case 'i': - iflag = 1; + iflag = true; break; case 'f': numfields = strtonum(optarg, 0, INT_MAX, &errstr); @@ -127,7 +123,7 @@ errx(1, "character skip value is %s: %s", errstr, optarg); break; case 'u': - uflag = 1; + uflag = true; break; case '?': default: @@ -140,6 +136,11 @@ if (argc > 2) usage(); + if (Dflag && dflag) + dflag = false; + if (uflag && cflag) + cflag = false; + ifp = stdin; ifn = "stdin"; ofp = stdout; @@ -180,6 +181,8 @@ err(1, "%s", ifn); exit(0); } + if (!cflag && !Dflag && !dflag && !uflag) + show(ofp, prevline); tprev = convert(prevline); tthis = NULL; @@ -199,7 +202,11 @@ /* If different, print; set previous to new value. */ if (Dflag == DF_POSTSEP && repeats > 0) fputc('\n', ofp); - if (!Dflag) + if (!cflag && !Dflag && !dflag && !uflag) + show(ofp, thisline); + else if (!Dflag && + (!dflag || (cflag && repeats > 0)) && + (!uflag || repeats == 0)) show(ofp, prevline); p = prevline; b1 = prevbuflen; @@ -220,13 +227,20 @@ show(ofp, prevline); } show(ofp, thisline); + } else if (dflag && !cflag) { + if (repeats == 0) + show(ofp, prevline); } ++repeats; } } if (ferror(ifp)) err(1, "%s", ifn); - if (!Dflag) + if (!cflag && !Dflag && !dflag && !uflag) + /* already printed */ ; + else if (!Dflag && + (!dflag || (cflag && repeats > 0)) && + (!uflag || repeats == 0)) show(ofp, prevline); exit(0); } @@ -291,11 +305,8 @@ static void show(FILE *ofp, const char *str) { - - if ((!Dflag && dflag && repeats == 0) || (uflag && repeats > 0)) - return; if (cflag) - (void)fprintf(ofp, "%4d %s", repeats + 1, str); + (void)fprintf(ofp, "%4lld %s", repeats + 1, str); else (void)fprintf(ofp, "%s", str); }