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
32 * @(#)fnmatch.c 8.2 (Berkeley) 4/16/94
33 * $FreeBSD: src/lib/libc/gen/fnmatch.c,v 1.18 2007/01/09 00:27:53 imp Exp $
34 * $DragonFly: src/lib/libc/gen/fnmatch.c,v 1.6 2005/11/13 00:07:42 swildner Exp $
38 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
39 * Compares a filename or pathname to a pattern.
43 * Some notes on multibyte character support:
44 * 1. Patterns with illegal byte sequences match nothing.
45 * 2. Illegal byte sequences in the "string" argument are handled by treating
46 * them as single-byte characters with a value of the first byte of the
47 * sequence cast to wchar_t.
48 * 3. Multibyte conversion state objects (mbstate_t) are passed around and
49 * used for most, but not all, conversions. Further work will be required
50 * to support state-dependent encodings.
64 #define RANGE_NOMATCH 0
65 #define RANGE_ERROR (-1)
67 static int rangematch(const char *, wchar_t, int, char **, mbstate_t *);
68 static int fnmatch1(const char *, const char *, int, mbstate_t, mbstate_t);
71 fnmatch(const char *pattern, const char *string, int flags)
73 static const mbstate_t initial;
75 return (fnmatch1(pattern, string, flags, initial, initial));
79 fnmatch1(const char *pattern, const char *string, int flags, mbstate_t patmbs,
82 const char *stringstart;
88 for (stringstart = string;;) {
89 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs);
90 if (pclen == (size_t)-1 || pclen == (size_t)-2)
93 sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
94 if (sclen == (size_t)-1 || sclen == (size_t)-2) {
95 sc = (unsigned char)*string;
97 memset(&strmbs, 0, sizeof(strmbs));
101 if ((flags & FNM_LEADING_DIR) && sc == '/')
103 return (sc == EOS ? 0 : FNM_NOMATCH);
106 return (FNM_NOMATCH);
107 if (sc == '/' && (flags & FNM_PATHNAME))
108 return (FNM_NOMATCH);
109 if (sc == '.' && (flags & FNM_PERIOD) &&
110 (string == stringstart ||
111 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
112 return (FNM_NOMATCH);
117 /* Collapse multiple stars. */
121 if (sc == '.' && (flags & FNM_PERIOD) &&
122 (string == stringstart ||
123 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
124 return (FNM_NOMATCH);
126 /* Optimize for pattern with * at end or before /. */
128 if (flags & FNM_PATHNAME)
129 return ((flags & FNM_LEADING_DIR) ||
130 strchr(string, '/') == NULL ?
134 else if (c == '/' && flags & FNM_PATHNAME) {
135 if ((string = strchr(string, '/')) == NULL)
136 return (FNM_NOMATCH);
140 /* General case, use recursion. */
142 if (!fnmatch1(pattern, string,
143 flags & ~FNM_PERIOD, patmbs, strmbs))
145 sclen = mbrtowc(&sc, string, MB_LEN_MAX,
147 if (sclen == (size_t)-1 ||
148 sclen == (size_t)-2) {
149 sc = (unsigned char)*string;
151 memset(&strmbs, 0, sizeof(strmbs));
153 if (sc == '/' && flags & FNM_PATHNAME)
157 return (FNM_NOMATCH);
160 return (FNM_NOMATCH);
161 if (sc == '/' && (flags & FNM_PATHNAME))
162 return (FNM_NOMATCH);
163 if (sc == '.' && (flags & FNM_PERIOD) &&
164 (string == stringstart ||
165 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
166 return (FNM_NOMATCH);
168 switch (rangematch(pattern, sc, flags, &newp,
176 return (FNM_NOMATCH);
181 if (!(flags & FNM_NOESCAPE)) {
182 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
184 if (pclen == (size_t)-1 || pclen == (size_t)-2)
185 return (FNM_NOMATCH);
195 else if ((flags & FNM_CASEFOLD) &&
196 (towlower(pc) == towlower(sc)))
199 return (FNM_NOMATCH);
208 rangematch(const char *pattern, wchar_t test, int flags, char **newp,
217 * A bracket expression starting with an unquoted circumflex
218 * character produces unspecified results (IEEE 1003.2-1992,
219 * 3.13.2). This implementation treats it like '!', for
220 * consistency with the regular expression syntax.
221 * J.T. Conklin (conklin@ngai.kaleida.com)
223 if ((negate = (*pattern == '!' || *pattern == '^')))
226 if (flags & FNM_CASEFOLD)
227 test = towlower(test);
230 * A right bracket shall lose its special meaning and represent
231 * itself in a bracket expression if it occurs first in the list.
237 if (*pattern == ']' && pattern > origpat) {
240 } else if (*pattern == '\0') {
241 return (RANGE_ERROR);
242 } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
243 return (RANGE_NOMATCH);
244 } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE)) {
247 pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
248 if (pclen == (size_t)-1 || pclen == (size_t)-2)
249 return (RANGE_NOMATCH);
252 if (flags & FNM_CASEFOLD)
255 if (*pattern == '-' && *(pattern + 1) != EOS &&
256 *(pattern + 1) != ']') {
257 if (*++pattern == '\\' && !(flags & FNM_NOESCAPE)) {
261 pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
262 if (pclen == (size_t)-1 || pclen == (size_t)-2)
263 return (RANGE_NOMATCH);
266 return (RANGE_ERROR);
268 if (flags & FNM_CASEFOLD)
271 if (__collate_load_error ?
272 c <= test && test <= c2 :
273 __collate_range_cmp(c, test) <= 0
274 && __collate_range_cmp(test, c2) <= 0
277 } else if (c == test) {
282 *newp = __DECONST(char *, pattern);
283 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);