+static int
+fnmatch_no_wildcards (char const *pattern, char const *f, int options)
+{
+ if (! (options & FNM_LEADING_DIR))
+ return ((options & FNM_CASEFOLD)
+ ? strcasecmp (pattern, f)
+ : strcmp (pattern, f));
+ else
+ {
+ size_t patlen = strlen (pattern);
+ int r = ((options & FNM_CASEFOLD)
+ ? strncasecmp (pattern, f, patlen)
+ : strncmp (pattern, f, patlen));
+ if (! r)
+ {
+ r = f[patlen];
+ if (r == '/')
+ r = 0;
+ }
+ return r;
+ }
+}
+
+/* Return true if EX excludes F. */
+
+bool
+excluded_filename (struct exclude const *ex, char const *f)
+{
+ size_t exclude_count = ex->exclude_count;
+
+ /* If no options are given, the default is to include. */
+ if (exclude_count == 0)
+ return false;
+ else
+ {
+ struct patopts const *exclude = ex->exclude;
+ size_t i;
+
+ /* Otherwise, the default is the opposite of the first option. */
+ bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
+
+ /* Scan through the options, seeing whether they change F from
+ excluded to included or vice versa. */
+ for (i = 0; i < exclude_count; i++)
+ {
+ char const *pattern = exclude[i].pattern;
+ int options = exclude[i].options;
+ if (excluded == !! (options & EXCLUDE_INCLUDE))
+ {
+ int (*matcher) PARAMS ((char const *, char const *, int)) =
+ (options & EXCLUDE_WILDCARDS
+ ? fnmatch
+ : fnmatch_no_wildcards);
+ bool matched = ((*matcher) (pattern, f, options) == 0);
+ char const *p;
+
+ if (! (options & EXCLUDE_ANCHORED))
+ for (p = f; *p && ! matched; p++)
+ if (*p == '/' && p[1] != '/')
+ matched = ((*matcher) (pattern, p + 1, options) == 0);
+
+ excluded ^= matched;
+ }
+ }
+
+ return excluded;
+ }