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 * $FreeBSD: src/sys/libkern/fnmatch.c,v 1.17 2003/06/11 05:23:04 obrien Exp $
37 * $DragonFly: src/sys/libkern/fnmatch.c,v 1.1 2004/01/06 00:56:47 dillon Exp $
41 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
42 * Compares a filename or pathname to a pattern.
45 #include <sys/param.h>
46 #include <sys/ctype.h>
47 #include <sys/libkern.h>
52 #define RANGE_NOMATCH 0
53 #define RANGE_ERROR (-1)
55 static int rangematch(const char *, char, int, char **);
58 fnmatch(pattern, string, flags)
59 const char *pattern, *string;
62 const char *stringstart;
66 for (stringstart = string;;)
67 switch (c = *pattern++) {
69 if ((flags & FNM_LEADING_DIR) && *string == '/')
71 return (*string == EOS ? 0 : FNM_NOMATCH);
75 if (*string == '/' && (flags & FNM_PATHNAME))
77 if (*string == '.' && (flags & FNM_PERIOD) &&
78 (string == stringstart ||
79 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
85 /* Collapse multiple stars. */
89 if (*string == '.' && (flags & FNM_PERIOD) &&
90 (string == stringstart ||
91 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
94 /* Optimize for pattern with * at end or before /. */
96 if (flags & FNM_PATHNAME)
97 return ((flags & FNM_LEADING_DIR) ||
98 index(string, '/') == NULL ?
102 else if (c == '/' && flags & FNM_PATHNAME) {
103 if ((string = index(string, '/')) == NULL)
104 return (FNM_NOMATCH);
108 /* General case, use recursion. */
109 while ((test = *string) != EOS) {
110 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
112 if (test == '/' && flags & FNM_PATHNAME)
116 return (FNM_NOMATCH);
119 return (FNM_NOMATCH);
120 if (*string == '/' && (flags & FNM_PATHNAME))
121 return (FNM_NOMATCH);
122 if (*string == '.' && (flags & FNM_PERIOD) &&
123 (string == stringstart ||
124 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
125 return (FNM_NOMATCH);
127 switch (rangematch(pattern, *string, flags, &newp)) {
134 return (FNM_NOMATCH);
139 if (!(flags & FNM_NOESCAPE)) {
140 if ((c = *pattern++) == EOS) {
150 else if ((flags & FNM_CASEFOLD) &&
151 (tolower((unsigned char)c) ==
152 tolower((unsigned char)*string)))
155 return (FNM_NOMATCH);
163 rangematch(pattern, test, flags, newp)
173 * A bracket expression starting with an unquoted circumflex
174 * character produces unspecified results (IEEE 1003.2-1992,
175 * 3.13.2). This implementation treats it like '!', for
176 * consistency with the regular expression syntax.
177 * J.T. Conklin (conklin@ngai.kaleida.com)
179 if ( (negate = (*pattern == '!' || *pattern == '^')) )
182 if (flags & FNM_CASEFOLD)
183 test = tolower((unsigned char)test);
186 * A right bracket shall lose its special meaning and represent
187 * itself in a bracket expression if it occurs first in the list.
193 if (c == '\\' && !(flags & FNM_NOESCAPE))
196 return (RANGE_ERROR);
198 if (c == '/' && (flags & FNM_PATHNAME))
199 return (RANGE_NOMATCH);
201 if (flags & FNM_CASEFOLD)
202 c = tolower((unsigned char)c);
205 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
207 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
210 return (RANGE_ERROR);
212 if (flags & FNM_CASEFOLD)
213 c2 = tolower((unsigned char)c2);
215 if (c <= test && test <= c2)
217 } else if (c == test)
219 } while ((c = *pattern++) != ']');
221 *newp = (char *)(uintptr_t)pattern;
222 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);