Author: kevans
Date: Sat Apr 21 13:46:07 2018
New Revision: 332856
URL: https://svnweb.freebsd.org/changeset/base/332856

Log:
  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

Modified:
  head/usr.bin/grep/util.c

Modified: head/usr.bin/grep/util.c
==============================================================================
--- head/usr.bin/grep/util.c    Sat Apr 21 09:58:00 2018        (r332855)
+++ head/usr.bin/grep/util.c    Sat Apr 21 13:46:07 2018        (r332856)
@@ -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);
 }
_______________________________________________
svn-src-head@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to