summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2011-12-24 02:16:47 -0800
committerJim Meyering <meyering@redhat.com>2011-12-24 12:55:50 +0100
commitadfe8bb24cbdb0c0feaf69e573ba6c023921cd33 (patch)
tree75ead772cb245444ee51d3217a5a2019ad52446a
parent5c24afd142b57d9378a1e0a735f8056d7592a800 (diff)
downloadgrep-adfe8bb24cbdb0c0feaf69e573ba6c023921cd33.tar.gz
--include etc. now work on command-line args more consistently
--include and --exclude apply only to non-directories and --exclude-dir applies only to directories. "-" (standard input) is never excluded, since it is not a file name. This bug was discovered while fixing a read-directory bug (Bug#10355). * NEWS: Document this. * src/main.c (main): Implement this. * tests/include-exclude: Test for it.
-rw-r--r--NEWS7
-rw-r--r--src/main.c27
-rwxr-xr-xtests/include-exclude6
3 files changed, 31 insertions, 9 deletions
diff --git a/NEWS b/NEWS
index 5e187f8a..32cb32ec 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,13 @@ GNU grep NEWS -*- outline -*-
** Bug fixes
+ The --include, --exclude, and --exclude-dir options now handle
+ command-line arguments more consistently. --include and --exclude
+ apply only to non-directories and --exclude-dir applies only to
+ directories. "-" (standard input) is never excluded, since it is
+ not a file name.
+ [bug introduced in grep-2.5]
+
grep no longer rejects "grep -qr . > out", i.e., when run with -q
and an input file is the same as the output file, since with -q
grep generates no output, so there is no risk of infinite loop or
diff --git a/src/main.c b/src/main.c
index afd77c5c..70197f42 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2205,15 +2205,26 @@ main (int argc, char **argv)
do
{
char *file = argv[optind];
- if ((included_patterns || excluded_patterns)
- && !isdir (file))
+ if (!STREQ (file, "-")
+ && (included_patterns || excluded_patterns
+ || excluded_directory_patterns))
{
- if (included_patterns
- && excluded_file_name (included_patterns, file))
- continue;
- if (excluded_patterns
- && excluded_file_name (excluded_patterns, file))
- continue;
+ if (isdir (file))
+ {
+ if (excluded_directory_patterns
+ && excluded_file_name (excluded_directory_patterns,
+ file))
+ continue;
+ }
+ else
+ {
+ if (included_patterns
+ && excluded_file_name (included_patterns, file))
+ continue;
+ if (excluded_patterns
+ && excluded_file_name (excluded_patterns, file))
+ continue;
+ }
}
status &= grepfile (STREQ (file, "-") ? (char *) NULL : file,
&stats_base);
diff --git a/tests/include-exclude b/tests/include-exclude
index 4249c56a..12955c00 100755
--- a/tests/include-exclude
+++ b/tests/include-exclude
@@ -12,6 +12,7 @@ printf '%s\n' x/dir/d:ddd > exp-not-ab || framework_failure_
printf '%s\n' x/a:aaa x/b:bbb > exp-not-d || framework_failure_
printf '%s\n' x/a:aaa x/b:bbb > exp-not-dir || framework_failure_
printf '%s\n' x/a:aaa > exp-a || framework_failure_
+printf '%s\n' aaa > exp-aaa || framework_failure_
grep -r --exclude='a*' . x > out || fail=1
sort out > k && mv k out
@@ -40,7 +41,10 @@ grep -r --include='a*' . x > out || fail=1
compare exp-a out || fail=1
# --include (without --recursive) uses different code
-grep --include=a '^aaa$' x/* > out || fail=1
+grep --include=a --exclude-dir=dir '^aaa$' x/* > out || fail=1
compare exp-a out || fail=1
+grep --exclude=- '^aaa$' - < x/a > out || fail=1
+compare exp-aaa out || fail=1
+
Exit $fail