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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
38 * Compares a filename or pathname to a pattern.
41 #include <sys/param.h>
42 #include <sys/ctype.h>
43 #include <sys/libkern.h>
48 #define RANGE_NOMATCH 0
49 #define RANGE_ERROR (-1)
51 static int rangematch(const char *, char, int, char **);
54 fnmatch(const char *pattern, const char *string, int flags)
56 const char *stringstart;
60 for (stringstart = string;;)
61 switch (c = *pattern++) {
63 if ((flags & FNM_LEADING_DIR) && *string == '/')
65 return (*string == EOS ? 0 : FNM_NOMATCH);
69 if (*string == '/' && (flags & FNM_PATHNAME))
71 if (*string == '.' && (flags & FNM_PERIOD) &&
72 (string == stringstart ||
73 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
79 /* Collapse multiple stars. */
83 if (*string == '.' && (flags & FNM_PERIOD) &&
84 (string == stringstart ||
85 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
88 /* Optimize for pattern with * at end or before /. */
90 if (flags & FNM_PATHNAME)
91 return ((flags & FNM_LEADING_DIR) ||
92 index(string, '/') == NULL ?
96 else if (c == '/' && flags & FNM_PATHNAME) {
97 if ((string = index(string, '/')) == NULL)
102 /* General case, use recursion. */
103 while ((test = *string) != EOS) {
104 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
106 if (test == '/' && flags & FNM_PATHNAME)
110 return (FNM_NOMATCH);
113 return (FNM_NOMATCH);
114 if (*string == '/' && (flags & FNM_PATHNAME))
115 return (FNM_NOMATCH);
116 if (*string == '.' && (flags & FNM_PERIOD) &&
117 (string == stringstart ||
118 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
119 return (FNM_NOMATCH);
121 switch (rangematch(pattern, *string, flags, &newp)) {
128 return (FNM_NOMATCH);
133 if (!(flags & FNM_NOESCAPE)) {
134 if ((c = *pattern++) == EOS) {
144 else if ((flags & FNM_CASEFOLD) &&
145 (tolower((unsigned char)c) ==
146 tolower((unsigned char)*string)))
149 return (FNM_NOMATCH);
157 rangematch(const char *pattern, char test, int flags, char **newp)
163 * A bracket expression starting with an unquoted circumflex
164 * character produces unspecified results (IEEE 1003.2-1992,
165 * 3.13.2). This implementation treats it like '!', for
166 * consistency with the regular expression syntax.
167 * J.T. Conklin (conklin@ngai.kaleida.com)
169 if ( (negate = (*pattern == '!' || *pattern == '^')) )
172 if (flags & FNM_CASEFOLD)
173 test = tolower((unsigned char)test);
176 * A right bracket shall lose its special meaning and represent
177 * itself in a bracket expression if it occurs first in the list.
183 if (c == '\\' && !(flags & FNM_NOESCAPE))
186 return (RANGE_ERROR);
188 if (c == '/' && (flags & FNM_PATHNAME))
189 return (RANGE_NOMATCH);
191 if (flags & FNM_CASEFOLD)
192 c = tolower((unsigned char)c);
195 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
197 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
200 return (RANGE_ERROR);
202 if (flags & FNM_CASEFOLD)
203 c2 = tolower((unsigned char)c2);
205 if (c <= test && test <= c2)
207 } else if (c == test)
209 } while ((c = *pattern++) != ']');
211 *newp = (char *)(uintptr_t)pattern;
212 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);