1 /* Copyright (C) 1991, 1992 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. */
21 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
29 /* Match STRING against the filename pattern PATTERN, returning zero if
30 it matches, nonzero if not. */
32 fnmatch (pattern, string, flags)
37 register const char *p = pattern, *n = string;
40 if ((flags & ~__FNM_FLAGS) != 0)
46 while ((c = *p++) != '\0')
53 else if ((flags & FNM_PATHNAME) && *n == '/')
55 else if ((flags & FNM_PERIOD) && *n == '.' &&
56 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
61 if (!(flags & FNM_NOESCAPE))
68 if ((flags & FNM_PERIOD) && *n == '.' &&
69 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
72 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
73 if (((flags & FNM_PATHNAME) && *n == '/') ||
74 (c == '?' && *n == '\0'))
81 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
82 for (--p; *n != '\0'; ++n)
83 if ((c == '[' || *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_PATHNAME) && n[-1] == '/')))
101 not = (*p == '!' || *p == '^');
108 register char cstart = c, cend = c;
110 if (!(flags & FNM_NOESCAPE) && c == '\\')
111 cstart = cend = *p++;
114 /* [ (unterminated) loses. */
119 if ((flags & FNM_PATHNAME) && c == '/')
120 /* [/] can never match. */
123 if (c == '-' && *p != ']')
126 if (!(flags & FNM_NOESCAPE) && cend == '\\')
133 if (*n >= cstart && *n <= cend)
144 /* Skip the rest of the [...] that already matched. */
148 /* [... (unterminated) loses. */
152 if (!(flags & FNM_NOESCAPE) && c == '\\')
153 /* 1003.2d11 is unclear if this is right. %%% */
169 if (*n == '\0' || ((flags & FNM_TARPATH) && *n == '/'))