diff --git a/src/grep.c b/src/grep.c
index eac540a..9fabeb8 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1386,7 +1386,8 @@ grep (int fd, struct stat const *st)
has_nulls = true;
if (binary_files == WITHOUT_MATCH_BINARY_FILES)
return 0;
- done_on_match = out_quiet = true;
+ if (!count_matches)
+ done_on_match = out_quiet = true;
nul_zapper = eol;
skip_nuls = skip_empty_lines;
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 2ade5be..2079ae5 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -106,6 +106,7 @@ TESTS = \
pcre \
pcre-abort \
pcre-context \
+ pcre-count \
pcre-infloop \
pcre-invalid-utf8-input \
pcre-jitstack \
diff --git a/tests/Makefile.in b/tests/Makefile.in
index b5bd7b5..ee516da 100644
--- a/tests/Makefile.in
+++ b/tests/Makefile.in
@@ -1442,6 +1442,7 @@ TESTS = \
pcre \
pcre-abort \
pcre-context \
+ pcre-count \
pcre-infloop \
pcre-invalid-utf8-input \
pcre-jitstack \
@@ -2250,6 +2251,13 @@ pcre-context.log: pcre-context
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
+pcre-count.log: pcre-count
+ @p='pcre-count'; \
+ b='pcre-count'; \
+ $(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
+ --log-file $$b.log --trs-file $$b.trs \
+ $(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
+ "$$tst" $(AM_TESTS_FD_REDIRECT)
pcre-infloop.log: pcre-infloop
@p='pcre-infloop'; \
b='pcre-infloop'; \
diff --git a/tests/pcre-count b/tests/pcre-count
new file mode 100755
index 0000000..78e1c7c
--- /dev/null
+++ b/tests/pcre-count
@@ -0,0 +1,23 @@
+#! /bin/sh
+# grep -P / grep -Pc are inconsistent results
+# This bug affected grep versions 2.21 through 2.22.
+#
+# Copyright (C) 2015 Free Software Foundation, Inc.
+#
+# Copying and distribution of this file, with or without modification,
+# are permitted in any medium without royalty provided the copyright
+# notice and this notice are preserved.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+require_pcre_
+
+fail=0
+
+printf 'a\n%032768d\nb\x0\n%032768d\na\n' 0 0 > in
+
+LC_ALL=C grep -P 'a' in | wc -l > exp
+
+LC_ALL=C grep -Pc 'a' in > out || fail=1
+compare exp out || fail=1
+
+Exit $fail