2 * Copyright (c) 2008, 2010 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.
41 #include <sys/types.h>
47 #endif /* HAVE_STRING_H */
50 #endif /* HAVE_STRINGS_H */
53 #include "compat/charclass.h"
54 #include "compat/fnmatch.h"
60 #define RANGE_NOMATCH 0
61 #define RANGE_ERROR (-1)
63 #if defined(LIBC_SCCS) && !defined(lint)
64 __unused static const char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
65 #endif /* LIBC_SCCS and not lint */
67 static int rangematch(const char *, int, int, char **);
68 static int classmatch(const char *, int, int, const char **);
71 rpl_fnmatch(const char *pattern, const char *string, int flags)
73 const char *stringstart;
77 for (stringstart = string;;)
78 switch (c = *pattern++) {
80 if (ISSET(flags, FNM_LEADING_DIR) && *string == '/')
82 return *string == EOS ? 0 : FNM_NOMATCH;
86 if (*string == '/' && ISSET(flags, FNM_PATHNAME))
88 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
89 (string == stringstart ||
90 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
96 /* Collapse multiple stars. */
100 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
101 (string == stringstart ||
102 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
105 /* Optimize for pattern with * at end or before /. */
107 if (ISSET(flags, FNM_PATHNAME))
108 return (ISSET(flags, FNM_LEADING_DIR) ||
109 strchr(string, '/') == NULL ?
113 } else if (c == '/' && ISSET(flags, FNM_PATHNAME)) {
114 if ((string = strchr(string, '/')) == NULL)
119 /* General case, use recursion. */
120 while ((test = *string) != EOS) {
121 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
123 if (test == '/' && ISSET(flags, FNM_PATHNAME))
131 if (*string == '/' && ISSET(flags, FNM_PATHNAME))
133 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
134 (string == stringstart ||
135 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
138 switch (rangematch(pattern, *string, flags, &newp)) {
140 /* not a good range, treat as normal text */
151 if (!ISSET(flags, FNM_NOESCAPE)) {
152 if ((c = *pattern++) == EOS) {
160 if (c != *string && !(ISSET(flags, FNM_CASEFOLD) &&
161 (tolower((unsigned char)c) ==
162 tolower((unsigned char)*string))))
171 rangematch(const char *pattern, int test, int flags, char **newp)
177 * A bracket expression starting with an unquoted circumflex
178 * character produces unspecified results (IEEE 1003.2-1992,
179 * 3.13.2). This implementation treats it like '!', for
180 * consistency with the regular expression syntax.
181 * J.T. Conklin (conklin@ngai.kaleida.com)
183 if ((negate = (*pattern == '!' || *pattern == '^')))
186 if (ISSET(flags, FNM_CASEFOLD))
187 test = tolower(test);
190 * A right bracket shall lose its special meaning and represent
191 * itself in a bracket expression if it occurs first in the list.
197 if (c == '[' && *pattern == ':') {
199 rv = classmatch(pattern + 1, test,
200 (flags & FNM_CASEFOLD), &pattern);
201 if (rv == RANGE_MATCH)
204 } while (rv != RANGE_ERROR && c == '[' && *pattern == ':');
208 if (c == '\\' && !ISSET(flags, FNM_NOESCAPE))
212 if (c == '/' && ISSET(flags, FNM_PATHNAME))
213 return RANGE_NOMATCH;
214 if (ISSET(flags, FNM_CASEFOLD))
215 c = tolower((unsigned char)c);
217 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
219 if (c2 == '\\' && !ISSET(flags, FNM_NOESCAPE))
223 if (ISSET(flags, FNM_CASEFOLD))
224 c2 = tolower((unsigned char)c2);
225 if (c <= test && test <= c2)
227 } else if (c == test)
229 } while ((c = *pattern++) != ']');
231 *newp = (char *)pattern;
232 return ok == negate ? RANGE_NOMATCH : RANGE_MATCH;
236 classmatch(const char *pattern, int test, int foldcase, const char **ep)
241 int rval = RANGE_NOMATCH;
243 if ((colon = strchr(pattern, ':')) == NULL || colon[1] != ']') {
248 len = (size_t)(colon - pattern);
250 if (foldcase && strncmp(pattern, "upper:]", 7) == 0)
252 for (cc = cclasses; cc->name != NULL; cc++) {
253 if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
254 if (cc->isctype(test))
259 if (cc->name == NULL) {
260 /* invalid character class, return EOS */
261 *ep = colon + strlen(colon);