2 * Copyright (c) 2008 Todd C. Miller <Todd.Miller@courtesan.com>
3 * Copyright (c) 1989, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
36 * Compares a filename or pathname to a pattern.
46 # ifdef HAVE_STRINGS_H
49 #endif /* HAVE_STRING_H */
52 #include "emul/fnmatch.h"
53 #include "emul/charclass.h"
59 #define RANGE_NOMATCH 0
60 #define RANGE_ERROR (-1)
62 #if defined(LIBC_SCCS) && !defined(lint)
63 __unused static const char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
64 #endif /* LIBC_SCCS and not lint */
66 static int rangematch __P((const char *, int, int, char **));
67 static int classmatch __P((const char *, int, int, const char **));
70 fnmatch(pattern, string, flags)
71 const char *pattern, *string;
74 const char *stringstart;
78 for (stringstart = string;;)
79 switch (c = *pattern++) {
81 if (ISSET(flags, FNM_LEADING_DIR) && *string == '/')
83 return (*string == EOS ? 0 : FNM_NOMATCH);
87 if (*string == '/' && ISSET(flags, FNM_PATHNAME))
89 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
90 (string == stringstart ||
91 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
97 /* Collapse multiple stars. */
101 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
102 (string == stringstart ||
103 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
104 return (FNM_NOMATCH);
106 /* Optimize for pattern with * at end or before /. */
108 if (ISSET(flags, FNM_PATHNAME))
109 return (ISSET(flags, FNM_LEADING_DIR) ||
110 strchr(string, '/') == NULL ?
114 } else if (c == '/' && ISSET(flags, FNM_PATHNAME)) {
115 if ((string = strchr(string, '/')) == NULL)
116 return (FNM_NOMATCH);
120 /* General case, use recursion. */
121 while ((test = *string) != EOS) {
122 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
124 if (test == '/' && ISSET(flags, FNM_PATHNAME))
128 return (FNM_NOMATCH);
131 return (FNM_NOMATCH);
132 if (*string == '/' && ISSET(flags, FNM_PATHNAME))
133 return (FNM_NOMATCH);
134 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
135 (string == stringstart ||
136 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
137 return (FNM_NOMATCH);
139 switch (rangematch(pattern, *string, flags, &newp)) {
141 /* not a good range, treat as normal text */
147 return (FNM_NOMATCH);
152 if (!ISSET(flags, FNM_NOESCAPE)) {
153 if ((c = *pattern++) == EOS) {
161 if (c != *string && !(ISSET(flags, FNM_CASEFOLD) &&
162 (tolower((unsigned char)c) ==
163 tolower((unsigned char)*string))))
164 return (FNM_NOMATCH);
173 rangematch(const char *pattern, int test, int flags, char **newp)
175 rangematch(pattern, test, flags, newp)
186 * A bracket expression starting with an unquoted circumflex
187 * character produces unspecified results (IEEE 1003.2-1992,
188 * 3.13.2). This implementation treats it like '!', for
189 * consistency with the regular expression syntax.
190 * J.T. Conklin (conklin@ngai.kaleida.com)
192 if ((negate = (*pattern == '!' || *pattern == '^')))
195 if (ISSET(flags, FNM_CASEFOLD))
196 test = tolower(test);
199 * A right bracket shall lose its special meaning and represent
200 * itself in a bracket expression if it occurs first in the list.
206 if (c == '[' && *pattern == ':') {
208 rv = classmatch(pattern + 1, test,
209 (flags & FNM_CASEFOLD), &pattern);
210 if (rv == RANGE_MATCH)
213 } while (rv != RANGE_ERROR && c == '[' && *pattern == ':');
217 if (c == '\\' && !ISSET(flags, FNM_NOESCAPE))
220 return (RANGE_ERROR);
221 if (c == '/' && ISSET(flags, FNM_PATHNAME))
222 return (RANGE_NOMATCH);
223 if (ISSET(flags, FNM_CASEFOLD))
224 c = tolower((unsigned char)c);
226 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
228 if (c2 == '\\' && !ISSET(flags, FNM_NOESCAPE))
231 return (RANGE_ERROR);
232 if (ISSET(flags, FNM_CASEFOLD))
233 c2 = tolower((unsigned char)c2);
234 if (c <= test && test <= c2)
236 } else if (c == test)
238 } while ((c = *pattern++) != ']');
240 *newp = (char *)pattern;
241 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
246 classmatch(const char *pattern, int test, int foldcase, const char **ep)
248 classmatch(pattern, test, foldcase, ep)
258 int rval = RANGE_NOMATCH;
260 if ((colon = strchr(pattern, ':')) == NULL || colon[1] != ']') {
265 len = (size_t)(colon - pattern);
267 if (foldcase && strncmp(pattern, "upper:]", 7) == 0)
269 for (cc = cclasses; cc->name != NULL; cc++) {
270 if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
271 if (cc->isctype(test))
276 if (cc->name == NULL) {
277 /* invalid character class, return EOS */
278 *ep = colon + strlen(colon);