1 /* Copyright (C) 1992 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details. */
14 /* Modified slightly by Brian Berliner <berliner@sun.com> and
15 Jim Blandy <jimb@cyclic.com> for CVS use */
24 /* #include <ansidecl.h> */
29 #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS)
33 /* Match STRING against the filename pattern PATTERN, returning zero if
34 it matches, nonzero if not. */
37 fnmatch (const char *pattern, const char *string, int flags)
39 fnmatch (pattern, string, flags)
45 register const char *p = pattern, *n = string;
48 if ((flags & ~__FNM_FLAGS) != 0)
54 while ((c = *p++) != '\0')
61 else if ((flags & FNM_PATHNAME) && *n == '/')
63 else if ((flags & FNM_PERIOD) && *n == '.' &&
64 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
69 if (!(flags & FNM_NOESCAPE))
71 if (FOLD_FN_CHAR (*n) != FOLD_FN_CHAR (c))
76 if ((flags & FNM_PERIOD) && *n == '.' &&
77 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
80 for (c = *p++; c == '?' || c == '*'; c = *p++, ++n)
81 if (((flags & FNM_PATHNAME) && *n == '/') ||
82 (c == '?' && *n == '\0'))
89 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
90 for (--p; *n != '\0'; ++n)
91 if ((c == '[' || FOLD_FN_CHAR (*n) == FOLD_FN_CHAR (c1)) &&
92 fnmatch(p, n, flags & ~FNM_PERIOD) == 0)
99 /* Nonzero if the sense of the character class is inverted. */
105 if ((flags & FNM_PERIOD) && *n == '.' &&
106 (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/')))
109 not = (*p == '!' || *p == '^');
116 register char cstart = c, cend = c;
118 if (!(flags & FNM_NOESCAPE) && c == '\\')
119 cstart = cend = *p++;
122 /* [ (unterminated) loses. */
127 if ((flags & FNM_PATHNAME) && c == '/')
128 /* [/] can never match. */
131 if (c == '-' && *p != ']')
134 if (!(flags & FNM_NOESCAPE) && cend == '\\')
141 if (*n >= cstart && *n <= cend)
152 /* Skip the rest of the [...] that already matched. */
156 /* [... (unterminated) loses. */
160 if (!(flags & FNM_NOESCAPE) && c == '\\')
161 /* 1003.2d11 is unclear if this is right. %%% */
170 if (FOLD_FN_CHAR (c) != FOLD_FN_CHAR (*n))