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 @@ -377,6 +377,20 @@ { atf_check -s exit:1 egrep '{' /dev/null } + +atf_test_case zerolen +zerolen_head() +{ + atf_set "descr" "Check for successful zero-length matches with ^$" +} +zerolen_body() +{ + printf "Eggs\n\nCheese" > test1 + + atf_check -o inline:"\n" grep -e "^$" test1 + + atf_check -o inline:"Eggs\nCheese\n" grep -v -e "^$" test1 +} # End FreeBSD atf_init_test_cases() @@ -404,5 +418,6 @@ atf_add_test_case f_file_empty atf_add_test_case escmap atf_add_test_case egrep_empty_invalid + atf_add_test_case zerolen # End FreeBSD } Index: head/usr.bin/grep/util.c =================================================================== --- head/usr.bin/grep/util.c +++ head/usr.bin/grep/util.c @@ -352,9 +352,6 @@ if (r == 0) { lastmatches++; lastmatch = pmatch; - /* Skip over zero-length matches */ - if (pmatch.rm_so == pmatch.rm_eo) - continue; if (m == 0) c++; @@ -532,6 +529,9 @@ /* --color and -o */ if ((oflag || color) && m > 0) { for (i = 0; i < m; i++) { + /* Don't output zero length matches */ + if (matches[i].rm_so == matches[i].rm_eo) + continue; if (!oflag) fwrite(line->dat + a, matches[i].rm_so - a, 1, stdout);