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 * @(#)fnmatch.c 8.2 (Berkeley) 4/16/94
37 * $DragonFly: src/lib/libc/gen/fnmatch.c,v 1.4 2003/11/12 20:21:23 eirikn Exp $
41 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
42 * Compares a filename or pathname to a pattern.
55 #define RANGE_NOMATCH 0
56 #define RANGE_ERROR (-1)
58 static int rangematch (const char *, char, int, char **);
61 fnmatch(pattern, string, flags)
62 const char *pattern, *string;
65 const char *stringstart;
69 for (stringstart = string;;)
70 switch (c = *pattern++) {
72 if ((flags & FNM_LEADING_DIR) && *string == '/')
74 return (*string == EOS ? 0 : FNM_NOMATCH);
78 if (*string == '/' && (flags & FNM_PATHNAME))
80 if (*string == '.' && (flags & FNM_PERIOD) &&
81 (string == stringstart ||
82 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
88 /* Collapse multiple stars. */
92 if (*string == '.' && (flags & FNM_PERIOD) &&
93 (string == stringstart ||
94 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
97 /* Optimize for pattern with * at end or before /. */
99 if (flags & FNM_PATHNAME)
100 return ((flags & FNM_LEADING_DIR) ||
101 strchr(string, '/') == NULL ?
105 else if (c == '/' && flags & FNM_PATHNAME) {
106 if ((string = strchr(string, '/')) == NULL)
107 return (FNM_NOMATCH);
111 /* General case, use recursion. */
112 while ((test = *string) != EOS) {
113 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
115 if (test == '/' && flags & FNM_PATHNAME)
119 return (FNM_NOMATCH);
122 return (FNM_NOMATCH);
123 if (*string == '/' && (flags & FNM_PATHNAME))
124 return (FNM_NOMATCH);
125 if (*string == '.' && (flags & FNM_PERIOD) &&
126 (string == stringstart ||
127 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
128 return (FNM_NOMATCH);
130 switch (rangematch(pattern, *string, flags, &newp)) {
137 return (FNM_NOMATCH);
142 if (!(flags & FNM_NOESCAPE)) {
143 if ((c = *pattern++) == EOS) {
153 else if ((flags & FNM_CASEFOLD) &&
154 (tolower((unsigned char)c) ==
155 tolower((unsigned char)*string)))
158 return (FNM_NOMATCH);
166 rangematch(pattern, test, flags, newp)
176 * A bracket expression starting with an unquoted circumflex
177 * character produces unspecified results (IEEE 1003.2-1992,
178 * 3.13.2). This implementation treats it like '!', for
179 * consistency with the regular expression syntax.
180 * J.T. Conklin (conklin@ngai.kaleida.com)
182 if ( (negate = (*pattern == '!' || *pattern == '^')) )
185 if (flags & FNM_CASEFOLD)
186 test = tolower((unsigned char)test);
189 * A right bracket shall lose its special meaning and represent
190 * itself in a bracket expression if it occurs first in the list.
196 if (c == '\\' && !(flags & FNM_NOESCAPE))
199 return (RANGE_ERROR);
201 if (c == '/' && (flags & FNM_PATHNAME))
202 return (RANGE_NOMATCH);
204 if (flags & FNM_CASEFOLD)
205 c = tolower((unsigned char)c);
208 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
210 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
213 return (RANGE_ERROR);
215 if (flags & FNM_CASEFOLD)
216 c2 = tolower((unsigned char)c2);
218 if (__collate_load_error ?
219 c <= test && test <= c2 :
220 __collate_range_cmp(c, test) <= 0
221 && __collate_range_cmp(test, c2) <= 0
224 } else if (c == test)
226 } while ((c = *pattern++) != ']');
228 *newp = (char *)pattern;
229 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);