1 /* Copyright (C) 1991, 1992, 1993 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. */
23 /* Comment out all this code if we are using the GNU C Library, and are not
24 actually compiling the library itself. This code is part of the GNU C
25 Library, but also included in many other GNU distributions. Compiling
26 and linking in this code is a waste when using the GNU C library
27 (especially if it is a shared library). Rather than having every GNU
28 program understand `configure --with-gnu-libc' and omit the object files,
29 it is simpler to just do this in the source for each such file. */
31 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
34 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
38 /* Match STRING against the filename pattern PATTERN, returning zero if
39 it matches, nonzero if not. */
41 fnmatch (pattern, string, flags)
46 register const char *p = pattern, *n = string;
49 /* Note that this evalutes C many times. */
50 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
52 while ((c = *p++) != '\0')
61 else if ((flags & FNM_FILE_NAME) && *n == '/')
63 else if ((flags & FNM_PERIOD) && *n == '.' &&
64 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
69 if (!(flags & FNM_NOESCAPE))
79 if ((flags & FNM_PERIOD) && *n == '.' &&
80 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
83 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
84 if (((flags & FNM_FILE_NAME) && *n == '/') ||
85 (c == '?' && *n == '\0'))
92 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
94 for (--p; *n != '\0'; ++n)
95 if ((c == '[' || FOLD (*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) && n[-1] == '/')))
113 not = (*p == '!' || *p == '^');
120 register char cstart = c, cend = c;
122 if (!(flags & FNM_NOESCAPE) && c == '\\')
123 cstart = cend = *p++;
125 cstart = cend = FOLD (cstart);
128 /* [ (unterminated) loses. */
134 if ((flags & FNM_FILE_NAME) && c == '/')
135 /* [/] can never match. */
138 if (c == '-' && *p != ']')
141 if (!(flags & FNM_NOESCAPE) && cend == '\\')
150 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
161 /* Skip the rest of the [...] that already matched. */
165 /* [... (unterminated) loses. */
169 if (!(flags & FNM_NOESCAPE) && c == '\\')
170 /* XXX 1003.2d11 is unclear if this is right. */
189 if ((flags & FNM_LEADING_DIR) && *n == '/')
190 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
196 #endif /* _LIBC or not __GNU_LIBRARY__. */