1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 /* Match STRING against the filename pattern PATTERN, returning zero if
25 it matches, nonzero if not. */
27 fnmatch (const char *pattern, const char *string, int flags)
29 register const char *p = pattern, *n = string;
32 /* Note that this evaluates C many times. */
33 # define FOLD(c) ((flags & FNM_CASEFOLD) && B_ISUPPER (c) ? tolower (c) : (c))
35 while ((c = *p++) != '\0')
44 else if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n))
46 else if ((flags & FNM_PERIOD) && *n == '.' &&
47 (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1]))))
52 if (!(flags & FNM_NOESCAPE))
56 /* Trailing \ loses. */
65 if ((flags & FNM_PERIOD) && *n == '.' &&
66 (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1]))))
69 for (c = *p++; c == '?' || c == '*'; c = *p++)
71 if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n))
72 /* A slash does not match a wildcard under FNM_FILE_NAME. */
76 /* A ? needs to match one character. */
78 /* There isn't another character; no match. */
81 /* One character of the string is consumed in matching
82 this ? wildcard, so *??? won't match if there are
83 less than three characters. */
92 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
94 for (--p; *n != '\0'; ++n)
95 if ((c == '[' || FOLD ((unsigned char)*n) == c1) &&
96 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
103 /* Nonzero if the sense of the character class is inverted. */
109 if ((flags & FNM_PERIOD) && *n == '.' &&
110 (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1]))))
113 nnot = (*p == '!' || *p == '^');
120 register char cstart = c, cend = c;
122 if (!(flags & FNM_NOESCAPE) && c == '\\')
126 cstart = cend = *p++;
129 cstart = cend = FOLD (cstart);
132 /* [ (unterminated) loses. */
138 if ((flags & FNM_FILE_NAME) && IsPathSeparator(c))
139 /* [/] can never match. */
142 if (c == '-' && *p != ']')
145 if (!(flags & FNM_NOESCAPE) && cend == '\\')
154 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
165 /* Skip the rest of the [...] that already matched. */
169 /* [... (unterminated) loses. */
173 if (!(flags & FNM_NOESCAPE) && c == '\\')
177 /* XXX 1003.2d11 is unclear if this is right. */
197 if ((flags & FNM_LEADING_DIR) && IsPathSeparator(*n))
198 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */