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. */
22 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
26 /* Match STRING against the filename pattern PATTERN, returning zero if
27 it matches, nonzero if not. */
29 fnmatch (pattern, string, flags)
34 register const char *p = pattern, *n = string;
37 /* Note that this evalutes C many times. */
38 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c))
40 while ((c = *p++) != '\0')
49 else if ((flags & FNM_FILE_NAME) && *n == '/')
51 else if ((flags & FNM_PERIOD) && *n == '.' &&
52 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
57 if (!(flags & FNM_NOESCAPE))
67 if ((flags & FNM_PERIOD) && *n == '.' &&
68 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
71 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
72 if (((flags & FNM_FILE_NAME) && *n == '/') ||
73 (c == '?' && *n == '\0'))
80 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
82 for (--p; *n != '\0'; ++n)
83 if ((c == '[' || FOLD (*n) == c1) &&
84 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
91 /* Nonzero if the sense of the character class is inverted. */
97 if ((flags & FNM_PERIOD) && *n == '.' &&
98 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
101 not = (*p == '!' || *p == '^');
108 register char cstart = c, cend = c;
110 if (!(flags & FNM_NOESCAPE) && c == '\\')
111 cstart = cend = *p++;
113 cstart = cend = FOLD (cstart);
116 /* [ (unterminated) loses. */
122 if ((flags & FNM_FILE_NAME) && c == '/')
123 /* [/] can never match. */
126 if (c == '-' && *p != ']')
129 if (!(flags & FNM_NOESCAPE) && cend == '\\')
138 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
149 /* Skip the rest of the [...] that already matched. */
153 /* [... (unterminated) loses. */
157 if (!(flags & FNM_NOESCAPE) && c == '\\')
158 /* XXX 1003.2d11 is unclear if this is right. */
177 if ((flags & FNM_LEADING_DIR) && *n == '/')
178 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */