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
This commit is contained in:
kevans 2018-04-21 13:46:07 +00:00
parent f139bcd72b
commit 985dd72ad9

View File

@ -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);
}