1 /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public License as
5 published by the Free Software Foundation; either version 2 of the
6 License, or (at your option) any later version.
8 This library 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 GNU
11 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public
14 License along with this library; see the file COPYING.LIB. If
15 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
16 Cambridge, MA 02139, USA. */
27 /* Comment out all this code if we are using the GNU C Library, and are not
28 actually compiling the library itself. This code is part of the GNU C
29 Library, but also included in many other GNU distributions. Compiling
30 and linking in this code is a waste when using the GNU C library
31 (especially if it is a shared library). Rather than having every GNU
32 program understand `configure --with-gnu-libc' and omit the object files,
33 it is simpler to just do this in the source for each such file. */
35 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
38 # if defined (STDC_HEADERS) || !defined (isascii)
41 # define ISASCII(c) isascii(c)
44 # define ISUPPER(c) (ISASCII (c) && isupper (c))
47 /* Match STRING against the filename pattern PATTERN, returning zero if
48 it matches, nonzero if not. */
49 int fnmatch(const char *pattern, const char *string, int flags)
51 register const char *p = pattern, *n = string;
54 /* Note that this evaluates C many times. */
55 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
57 while ((c = *p++) != '\0') {
64 else if ((flags & FNM_FILE_NAME) && *n == '/')
66 else if ((flags & FNM_PERIOD) && *n == '.' &&
68 || ((flags & FNM_FILE_NAME)
69 && n[-1] == '/'))) return FNM_NOMATCH;
73 if (!(flags & FNM_NOESCAPE)) {
76 /* Trailing \ loses. */
85 if ((flags & FNM_PERIOD) && *n == '.' &&
86 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
89 for (c = *p++; c == '?' || c == '*'; c = *p++) {
90 if ((flags & FNM_FILE_NAME) && *n == '/')
91 /* A slash does not match a wildcard under FNM_FILE_NAME. */
94 /* A ? needs to match one character. */
96 /* There isn't another character; no match. */
99 /* One character of the string is consumed in matching
100 this ? wildcard, so *??? won't match if there are
101 less than three characters. */
110 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
113 for (--p; *n != '\0'; ++n)
114 if ((c == '[' || FOLD(*n) == c1) &&
115 fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
122 /* Nonzero if the sense of the character class is inverted. */
128 if ((flags & FNM_PERIOD) && *n == '.' &&
129 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
132 not = (*p == '!' || *p == '^');
138 register char cstart = c, cend = c;
140 if (!(flags & FNM_NOESCAPE) && c == '\\') {
143 cstart = cend = *p++;
146 cstart = cend = FOLD(cstart);
149 /* [ (unterminated) loses. */
155 if ((flags & FNM_FILE_NAME) && c == '/')
156 /* [/] can never match. */
159 if (c == '-' && *p != ']') {
161 if (!(flags & FNM_NOESCAPE) && cend == '\\')
170 if (FOLD(*n) >= cstart && FOLD(*n) <= cend)
181 /* Skip the rest of the [...] that already matched. */
184 /* [... (unterminated) loses. */
188 if (!(flags & FNM_NOESCAPE) && c == '\\') {
191 /* XXX 1003.2d11 is unclear if this is right. */
211 if ((flags & FNM_LEADING_DIR) && *n == '/')
212 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
219 libc_hidden_def(fnmatch)
220 #endif /* _LIBC or not __GNU_LIBRARY__. */