From ff415f05bff4054c1f9b72afb93e67db93b6e890 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Sat, 21 Apr 2018 13:46:07 +0000 Subject: [PATCH] bsdgrep: Fix --include/--exclude ordering issues Prior to r332851: * --exclude always win out over --include * --exclude-dir always wins out over --include-dir r332851 broke that behavior, resulting in: * First of --exclude, --include wins * First of --exclude-dir, --include-dir wins As it turns out, both behaviors are wrong by modern grep standards- the latest rule wins. e.g.: `grep --exclude foo --include foo 'thing' foo` foo is included `grep --include foo --exclude foo 'thing' foo` foo is excluded As tested with GNU grep 3.1. This commit makes bsdgrep follow this behavior. Reported by: se --- usr.bin/grep/util.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index 557e8d7ab19a..07067a9d3356 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -109,10 +109,12 @@ file_matching(const char *fname) for (unsigned int i = 0; i < fpatterns; ++i) { if (fnmatch(fpattern[i].pat, fname, 0) == 0 || - fnmatch(fpattern[i].pat, fname_base, 0) == 0) { + fnmatch(fpattern[i].pat, fname_base, 0) == 0) + /* + * The last pattern matched wins exclusion/inclusion + * rights, so we can't reasonably bail out early here. + */ ret = (fpattern[i].mode != EXCL_PAT); - break; - } } free(fname_buf); return (ret); @@ -127,7 +129,11 @@ dir_matching(const char *dname) for (unsigned int i = 0; i < dpatterns; ++i) { if (dname != NULL && fnmatch(dpattern[i].pat, dname, 0) == 0) - return (dpattern[i].mode != EXCL_PAT); + /* + * The last pattern matched wins exclusion/inclusion + * rights, so we can't reasonably bail out early here. + */ + ret = (dpattern[i].mode != EXCL_PAT); } return (ret); }