2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * From FreeBSD fnmatch.c 1.11
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
42 #endif /* LIBC_SCCS and not lint */
45 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
46 * Compares a filename or pathname to a pattern.
57 static const char *rangematch(const char *, char, int);
59 int fnmatch(const char *pattern, const char *string, int flags)
61 const char *stringstart;
64 for (stringstart = string;;)
65 switch (c = *pattern++) {
67 if ((flags & FNM_LEADING_DIR) && *string == '/')
69 return (*string == EOS ? 0 : FNM_NOMATCH);
73 if (*string == '/' && (flags & FNM_PATHNAME))
75 if (*string == '.' && (flags & FNM_PERIOD) &&
76 (string == stringstart ||
77 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
83 /* Collapse multiple stars. */
87 if (*string == '.' && (flags & FNM_PERIOD) &&
88 (string == stringstart ||
89 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
92 /* Optimize for pattern with * at end or before /. */
94 if (flags & FNM_PATHNAME)
95 return ((flags & FNM_LEADING_DIR) ||
96 strchr(string, '/') == NULL ?
100 else if (c == '/' && flags & FNM_PATHNAME) {
101 if ((string = strchr(string, '/')) == NULL)
102 return (FNM_NOMATCH);
106 /* General case, use recursion. */
107 while ((test = *string) != EOS) {
108 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
110 if (test == '/' && flags & FNM_PATHNAME)
114 return (FNM_NOMATCH);
117 return (FNM_NOMATCH);
118 if (*string == '/' && flags & FNM_PATHNAME)
119 return (FNM_NOMATCH);
121 rangematch(pattern, *string, flags)) == NULL)
122 return (FNM_NOMATCH);
126 if (!(flags & FNM_NOESCAPE)) {
127 if ((c = *pattern++) == EOS) {
136 else if ((flags & FNM_CASEFOLD) &&
137 (tolower((unsigned char)c) ==
138 tolower((unsigned char)*string)))
140 else if ((flags & FNM_PREFIX_DIRS) && *string == EOS &&
141 ((c == '/' && string != stringstart) ||
142 (string == stringstart+1 && *stringstart == '/')) )
145 return (FNM_NOMATCH);
153 rangematch(const char *pattern, char test, int flags)
159 * A bracket expression starting with an unquoted circumflex
160 * character produces unspecified results (IEEE 1003.2-1992,
161 * 3.13.2). This implementation treats it like '!', for
162 * consistency with the regular expression syntax.
163 * J.T. Conklin (conklin@ngai.kaleida.com)
165 if ( (negate = (*pattern == '!' || *pattern == '^')) )
168 if (flags & FNM_CASEFOLD)
169 test = tolower((unsigned char)test);
171 for (ok = 0; (c = *pattern++) != ']';) {
172 if (c == '\\' && !(flags & FNM_NOESCAPE))
177 if (flags & FNM_CASEFOLD)
178 c = tolower((unsigned char)c);
181 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
183 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
188 if (flags & FNM_CASEFOLD)
189 c2 = tolower((unsigned char)c2);
191 if ((unsigned char)c <= (unsigned char)test &&
192 (unsigned char)test <= (unsigned char)c2)
194 } else if (c == test)
197 return (ok == negate ? NULL : pattern);