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. */
21 /* Comment out all this code if we are using the GNU C Library, and are not
22 actually compiling the library itself. This code is part of the GNU C
23 Library, but also included in many other GNU distributions. Compiling
24 and linking in this code is a waste when using the GNU C library
25 (especially if it is a shared library). Rather than having every GNU
26 program understand `configure --with-gnu-libc' and omit the object files,
27 it is simpler to just do this in the source for each such file. */
29 #if defined _LIBC || !defined __GNU_LIBRARY__
32 # if defined STDC_HEADERS || !defined isascii
35 # define ISASCII(c) isascii(c)
39 # define ISUPPER(c) (ISASCII (c) && isupper (c))
47 /* Match STRING against the filename pattern PATTERN, returning zero if
48 it matches, nonzero if not. */
50 fnmatch (const char *pattern, const char *string, int flags)
52 register const char *p = pattern, *n = string;
55 /* Note that this evaluates C many times. */
56 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
58 while ((c = *p++) != '\0')
67 else if ((flags & FNM_FILE_NAME) && *n == '/')
69 else if ((flags & FNM_PERIOD) && *n == '.' &&
70 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
75 if (!(flags & FNM_NOESCAPE))
79 /* Trailing \ loses. */
88 if ((flags & FNM_PERIOD) && *n == '.' &&
89 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
92 for (c = *p++; c == '?' || c == '*'; c = *p++)
94 if ((flags & FNM_FILE_NAME) && *n == '/')
95 /* A slash does not match a wildcard under FNM_FILE_NAME. */
99 /* A ? needs to match one character. */
101 /* There isn't another character; no match. */
104 /* One character of the string is consumed in matching
105 this ? wildcard, so *??? won't match if there are
106 less than three characters. */
115 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
117 for (--p; *n != '\0'; ++n)
118 if ((c == '[' || FOLD (*n) == c1) &&
119 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
126 /* Nonzero if the sense of the character class is inverted. */
132 if ((flags & FNM_PERIOD) && *n == '.' &&
133 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
136 nnot = (*p == '!' || *p == '^');
143 register char cstart = c, cend = c;
145 if (!(flags & FNM_NOESCAPE) && c == '\\')
149 cstart = cend = *p++;
152 cstart = cend = FOLD (cstart);
155 /* [ (unterminated) loses. */
161 if ((flags & FNM_FILE_NAME) && c == '/')
162 /* [/] can never match. */
165 if (c == '-' && *p != ']')
168 if (!(flags & FNM_NOESCAPE) && cend == '\\')
177 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
188 /* Skip the rest of the [...] that already matched. */
192 /* [... (unterminated) loses. */
196 if (!(flags & FNM_NOESCAPE) && c == '\\')
200 /* XXX 1003.2d11 is unclear if this is right. */
220 if ((flags & FNM_LEADING_DIR) && *n == '/')
221 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
229 #endif /* _LIBC or not __GNU_LIBRARY__. */