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. */
17 /* Modified version of fnmatch.c - Robert Nelson */
20 #include "enh_fnmatch.h"
27 enh_fnmatch_sub(const char *pattern, const char *string, int patternlen, 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 ((p - pattern) < patternlen)
45 else if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n))
47 else if ((flags & FNM_PERIOD) && *n == '.' &&
48 (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1]))))
53 if (!(flags & FNM_NOESCAPE))
55 if ((p - pattern) >= patternlen)
56 /* Trailing \ loses. */
67 if ((flags & FNM_PERIOD) && *n == '.' &&
68 (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1]))))
71 if ((p - pattern) >= patternlen)
74 for (c = *p++; ((p - pattern) <= patternlen) && (c == '?' || c == '*'); c = *p++)
76 if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n))
77 /* A slash does not match a wildcard under FNM_FILE_NAME. */
81 /* A ? needs to match one character. */
83 /* There isn't another character; no match. */
86 /* One character of the string is consumed in matching
87 this ? wildcard, so *??? won't match if there are
88 less than three characters. */
93 if ((p - pattern) >= patternlen)
97 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
99 for (--p; *n != '\0'; ++n)
101 if (c == '[' || c == '{' || FOLD((unsigned char)*n) == c1)
105 len = enh_fnmatch_sub(p, n, (int)(patternlen - (p - pattern)), flags & ~FNM_PERIOD);
107 if (len > 0 && n[len] == '\0')
108 return (int)(n - string + len);
112 if ((flags & FNM_FILE_NAME) && IsPathSeparator(*n))
113 return 0; /* A slash does not match a wildcard under FNM_FILE_NAME. */
122 const char *pstart = p;
124 while ((p - pattern) < patternlen)
128 if (!(flags & FNM_NOESCAPE) && c == '\\')
130 if ((p - pattern) >= patternlen)
137 if (c == ',' || c == '}')
141 matchlen = enh_fnmatch_sub(pstart, n, (int)(p - pstart - 1), flags & ~FNM_PERIOD);
148 if (!(flags & FNM_NOESCAPE) && c == '\\')
150 if ((p - pattern) >= patternlen)
156 if ((p - pattern) >= patternlen)
175 /* Nonzero if the sense of the character class is inverted. */
181 if ((flags & FNM_PERIOD) && *n == '.' &&
182 (n == string || ((flags & FNM_FILE_NAME) && IsPathSeparator(n[-1]))))
185 nnot = (*p == '!' || *p == '^');
189 if ((p - pattern) >= patternlen)
190 /* [ (unterminated) loses. */
197 register char cstart, cend;
199 cstart = cend = FOLD (c);
201 if ((p - pattern) >= patternlen)
202 /* [ (unterminated) loses. */
208 if ((flags & FNM_FILE_NAME) && IsPathSeparator(c))
209 /* [/] can never match. */
212 if (c == '-' && *p != ']')
214 if ((p - pattern) >= patternlen)
221 if ((p - pattern) >= patternlen)
227 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
238 /* Skip the rest of the [...] that already matched. */
241 if ((p - pattern) >= patternlen)
260 return (int)(n - string);
265 /* Match STRING against the filename pattern PATTERN, returning number of characters
266 in STRING that were matched if all of PATTERN matches, nonzero if not. */
268 enh_fnmatch(const char *pattern, const char *string, int flags)
272 matchlen = enh_fnmatch_sub(pattern, string, (int)strlen(pattern), flags);
277 if (string[matchlen] == '\0')
280 if ((flags & FNM_LEADING_DIR) && IsPathSeparator(string[matchlen]))
281 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */