From fc6fce9a16bc52aca11cd8f1ad2632943fe94201 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 17 Nov 2016 21:05:42 -0800 Subject: [PATCH 07/10] grep: treat -f /dev/null like -m0 * NEWS: Document this. * src/grep.c (main): With -f /dev/null, don't bother to read the input. This is what FreeBSD grep does. * tests/Makefile.am (TESTS): Add skip-read. * tests/skip-read: New file. --- NEWS | 5 +++++ src/grep.c | 5 ++++- tests/Makefile.am | 1 + tests/skip-read | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100755 tests/skip-read diff --git a/NEWS b/NEWS index a165377..06a186a 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,11 @@ GNU grep NEWS -*- outline -*- grep's use of getprogname no longer causes a build failure on HP-UX. +** Improvements + + grep no longer reads the input in a few more cases when it is easy + to see that matching cannot succeed, e.g., 'grep -f /dev/null'. + * Noteworthy changes in release 2.26 (2016-10-02) [stable] diff --git a/src/grep.c b/src/grep.c index fe56e9c..317a0d5 100644 --- a/src/grep.c +++ b/src/grep.c @@ -2866,7 +2866,10 @@ main (int argc, char **argv) if (O_BINARY && !isatty (STDOUT_FILENO)) set_binary_mode (STDOUT_FILENO, O_BINARY); - if (max_count == 0) + /* If it is easy to see that matching cannot succeed (e.g., 'grep -f + /dev/null'), fail without reading the input. */ + if (max_count == 0 + || (keycc == 0 && out_invert && !match_lines && !match_words)) return EXIT_FAILURE; /* Prefer sysconf for page size, as getpagesize typically returns int. */ diff --git a/tests/Makefile.am b/tests/Makefile.am index f4c82f4..b6f0df3 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -143,6 +143,7 @@ TESTS = \ reversed-range-endpoints \ sjis-mb \ skip-device \ + skip-read \ spencer1 \ spencer1-locale \ status \ diff --git a/tests/skip-read b/tests/skip-read new file mode 100755 index 0000000..627d362 --- /dev/null +++ b/tests/skip-read @@ -0,0 +1,15 @@ +#!/bin/sh +# Check that grep skips reading in some cases. + +. "${srcdir=.}/init.sh"; path_prepend_ ../src + +fail=0 + +for opts in '-m0 y' '-f /dev/null' '-v ""'; do + for matcher in '' -E -F; do + eval returns_ 1 grep $opts $matcher no-such-file > out || fail=1 + compare /dev/null out || fail=1 + done +done + +Exit $fail -- 2.7.4