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 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
40 #endif /* LIBC_SCCS and not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
46 * Compares a filename or pathname to a pattern.
50 * Some notes on multibyte character support:
51 * 1. Patterns with illegal byte sequences match nothing.
52 * 2. Illegal byte sequences in the "string" argument are handled by treating
53 * them as single-byte characters with a value of the first byte of the
54 * sequence cast to wchar_t.
55 * 3. Multibyte conversion state objects (mbstate_t) are passed around and
56 * used for most, but not all, conversions. Further work will be required
57 * to support state-dependent encodings.
71 #define RANGE_NOMATCH 0
72 #define RANGE_ERROR (-1)
74 static int rangematch(const char *, wchar_t, int, char **, mbstate_t *);
75 static int fnmatch1(const char *, const char *, const char *, int, mbstate_t,
79 fnmatch(pattern, string, flags)
80 const char *pattern, *string;
83 static const mbstate_t initial;
85 return (fnmatch1(pattern, string, string, flags, initial, initial));
89 fnmatch1(pattern, string, stringstart, flags, patmbs, strmbs)
90 const char *pattern, *string, *stringstart;
92 mbstate_t patmbs, strmbs;
94 const char *bt_pattern, *bt_string;
95 mbstate_t bt_patmbs, bt_strmbs;
101 bt_pattern = bt_string = NULL;
103 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs);
104 if (pclen == (size_t)-1 || pclen == (size_t)-2)
105 return (FNM_NOMATCH);
107 sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
108 if (sclen == (size_t)-1 || sclen == (size_t)-2) {
109 sc = (unsigned char)*string;
111 memset(&strmbs, 0, sizeof(strmbs));
115 if ((flags & FNM_LEADING_DIR) && sc == '/')
122 return (FNM_NOMATCH);
123 if (sc == '/' && (flags & FNM_PATHNAME))
125 if (sc == '.' && (flags & FNM_PERIOD) &&
126 (string == stringstart ||
127 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
133 /* Collapse multiple stars. */
137 if (sc == '.' && (flags & FNM_PERIOD) &&
138 (string == stringstart ||
139 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
142 /* Optimize for pattern with * at end or before /. */
144 if (flags & FNM_PATHNAME)
145 return ((flags & FNM_LEADING_DIR) ||
146 strchr(string, '/') == NULL ?
150 else if (c == '/' && flags & FNM_PATHNAME) {
151 if ((string = strchr(string, '/')) == NULL)
152 return (FNM_NOMATCH);
157 * First try the shortest match for the '*' that
158 * could work. We can forget any earlier '*' since
159 * there is no way having it match more characters
160 * can help us, given that we are already here.
162 bt_pattern = pattern, bt_patmbs = patmbs;
163 bt_string = string, bt_strmbs = strmbs;
167 return (FNM_NOMATCH);
168 if (sc == '/' && (flags & FNM_PATHNAME))
170 if (sc == '.' && (flags & FNM_PERIOD) &&
171 (string == stringstart ||
172 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
175 switch (rangematch(pattern, sc, flags, &newp,
188 if (!(flags & FNM_NOESCAPE)) {
189 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
191 if (pclen == 0 || pclen == (size_t)-1 ||
193 return (FNM_NOMATCH);
202 else if ((flags & FNM_CASEFOLD) &&
203 (towlower(pc) == towlower(sc)))
208 * If we have a mismatch (other than hitting
209 * the end of the string), go back to the last
210 * '*' seen and have it match one additional
213 if (bt_pattern == NULL)
214 return (FNM_NOMATCH);
215 sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX,
217 if (sclen == (size_t)-1 ||
218 sclen == (size_t)-2) {
219 sc = (unsigned char)*bt_string;
221 memset(&bt_strmbs, 0,
225 return (FNM_NOMATCH);
226 if (sc == '/' && flags & FNM_PATHNAME)
227 return (FNM_NOMATCH);
229 pattern = bt_pattern, patmbs = bt_patmbs;
230 string = bt_string, strmbs = bt_strmbs;
239 rangematch(pattern, test, flags, newp, patmbs)
250 struct xlocale_collate *table =
251 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
254 * A bracket expression starting with an unquoted circumflex
255 * character produces unspecified results (IEEE 1003.2-1992,
256 * 3.13.2). This implementation treats it like '!', for
257 * consistency with the regular expression syntax.
258 * J.T. Conklin (conklin@ngai.kaleida.com)
260 if ( (negate = (*pattern == '!' || *pattern == '^')) )
263 if (flags & FNM_CASEFOLD)
264 test = towlower(test);
267 * A right bracket shall lose its special meaning and represent
268 * itself in a bracket expression if it occurs first in the list.
274 if (*pattern == ']' && pattern > origpat) {
277 } else if (*pattern == '\0') {
278 return (RANGE_ERROR);
279 } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
280 return (RANGE_NOMATCH);
281 } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
283 pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
284 if (pclen == (size_t)-1 || pclen == (size_t)-2)
285 return (RANGE_NOMATCH);
288 if (flags & FNM_CASEFOLD)
291 if (*pattern == '-' && *(pattern + 1) != EOS &&
292 *(pattern + 1) != ']') {
293 if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
296 pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
297 if (pclen == (size_t)-1 || pclen == (size_t)-2)
298 return (RANGE_NOMATCH);
301 return (RANGE_ERROR);
303 if (flags & FNM_CASEFOLD)
306 if (table->__collate_load_error ?
307 c <= test && test <= c2 :
308 __wcollate_range_cmp(c, test) <= 0
309 && __wcollate_range_cmp(test, c2) <= 0
312 } else if (c == test)
316 *newp = (char *)pattern;
317 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);