Index: head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh =================================================================== --- head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh +++ head/contrib/netbsd-tests/usr.bin/grep/t_grep.sh @@ -494,6 +494,34 @@ atf_check -s exit:1 grep -v -w "x" test1 atf_check -s exit:1 grep -v -w "x" test2 } + +atf_test_case grep_nomatch_flags +grep_nomatch_flags_head() +{ + atf_set "descr" "Check for no match (-c, -l, -L, -q) flags not producing line matches or context (PR 219077)" +} + +grep_nomatch_flags_body() +{ + printf "A\nB\nC\n" > test1 + + atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1 + atf_check -o inline:"1\n" grep -c -B 1 -e "B" test1 + atf_check -o inline:"1\n" grep -c -A 1 -e "B" test1 + atf_check -o inline:"1\n" grep -c -C 1 -e "B" test1 + + atf_check -o inline:"test1\n" grep -l -e "B" test1 + atf_check -o inline:"test1\n" grep -l -B 1 -e "B" test1 + atf_check -o inline:"test1\n" grep -l -A 1 -e "B" test1 + atf_check -o inline:"test1\n" grep -l -C 1 -e "B" test1 + + atf_check -s exit:1 -o inline:"test1\n" grep -L -e "D" test1 + + atf_check -o empty grep -q -e "B" test1 + atf_check -o empty grep -q -B 1 -e "B" test1 + atf_check -o empty grep -q -A 1 -e "B" test1 + atf_check -o empty grep -q -C 1 -e "B" test1 +} # End FreeBSD atf_init_test_cases() @@ -527,5 +555,6 @@ atf_add_test_case fgrep_sanity atf_add_test_case egrep_sanity atf_add_test_case grep_sanity + atf_add_test_case grep_nomatch_flags # End FreeBSD } Index: head/usr.bin/grep/util.c =================================================================== --- head/usr.bin/grep/util.c +++ head/usr.bin/grep/util.c @@ -201,7 +201,7 @@ struct str *ln; mode_t s; int c, last_outed, t, tail; - bool doctx, same_file; + bool doctx, printmatch, same_file; if (strcmp(fn, "-") == 0) { fn = label != NULL ? label : getstr(1); @@ -237,12 +237,14 @@ last_outed = 0; same_file = false; doctx = false; - if ((!pc.binary || binbehave != BINFILE_BIN) && !cflag && !qflag && - !lflag && !Lflag && (Aflag != 0 || Bflag != 0)) + printmatch = true; + if ((pc.binary && binbehave == BINFILE_BIN) || cflag || qflag || + lflag || Lflag) + printmatch = false; + if (printmatch && (Aflag != 0 || Bflag != 0)) doctx = true; mcount = mlimit; - for (c = 0; c == 0 || !(lflag || qflag); ) { /* Reset match count for every line processed */ pc.matchidx = 0; @@ -283,7 +285,7 @@ tail = Aflag; } /* Print the matching line, but only if not quiet/binary */ - if (t == 0 && !qflag && !pc.binary) { + if (t == 0 && printmatch) { printline(&pc, ':'); first_match = false; same_file = true;