2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Copyright (c) 2011 The FreeBSD Foundation
11 * All rights reserved.
12 * Portions of this software were developed by David Chisnall
13 * under sponsorship from the FreeBSD Foundation.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
42 #endif /* LIBC_SCCS and not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
48 * Compares a filename or pathname to a pattern.
52 * Some notes on multibyte character support:
53 * 1. Patterns with illegal byte sequences match nothing.
54 * 2. Illegal byte sequences in the "string" argument are handled by treating
55 * them as single-byte characters with a value of the first byte of the
56 * sequence cast to wchar_t.
57 * 3. Multibyte conversion state objects (mbstate_t) are passed around and
58 * used for most, but not all, conversions. Further work will be required
59 * to support state-dependent encodings.
73 #define RANGE_NOMATCH 0
74 #define RANGE_ERROR (-1)
76 static int rangematch(const char *, wchar_t, int, char **, mbstate_t *);
77 static int fnmatch1(const char *, const char *, const char *, int, mbstate_t,
81 fnmatch(const char *pattern, const char *string, int flags)
83 static const mbstate_t initial;
85 return (fnmatch1(pattern, string, string, flags, initial, initial));
89 fnmatch1(const char *pattern, const char *string, const char *stringstart,
90 int flags, mbstate_t patmbs, mbstate_t strmbs)
92 const char *bt_pattern, *bt_string;
93 mbstate_t bt_patmbs, bt_strmbs;
99 bt_pattern = bt_string = NULL;
101 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs);
102 if (pclen == (size_t)-1 || pclen == (size_t)-2)
103 return (FNM_NOMATCH);
105 sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
106 if (sclen == (size_t)-1 || sclen == (size_t)-2) {
107 sc = (unsigned char)*string;
109 memset(&strmbs, 0, sizeof(strmbs));
113 if ((flags & FNM_LEADING_DIR) && sc == '/')
120 return (FNM_NOMATCH);
121 if (sc == '/' && (flags & FNM_PATHNAME))
123 if (sc == '.' && (flags & FNM_PERIOD) &&
124 (string == stringstart ||
125 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
131 /* Collapse multiple stars. */
135 if (sc == '.' && (flags & FNM_PERIOD) &&
136 (string == stringstart ||
137 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
140 /* Optimize for pattern with * at end or before /. */
142 if (flags & FNM_PATHNAME)
143 return ((flags & FNM_LEADING_DIR) ||
144 strchr(string, '/') == NULL ?
148 else if (c == '/' && flags & FNM_PATHNAME) {
149 if ((string = strchr(string, '/')) == NULL)
150 return (FNM_NOMATCH);
155 * First try the shortest match for the '*' that
156 * could work. We can forget any earlier '*' since
157 * there is no way having it match more characters
158 * can help us, given that we are already here.
160 bt_pattern = pattern, bt_patmbs = patmbs;
161 bt_string = string, bt_strmbs = strmbs;
165 return (FNM_NOMATCH);
166 if (sc == '/' && (flags & FNM_PATHNAME))
168 if (sc == '.' && (flags & FNM_PERIOD) &&
169 (string == stringstart ||
170 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
173 switch (rangematch(pattern, sc, flags, &newp,
186 if (!(flags & FNM_NOESCAPE)) {
187 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
189 if (pclen == 0 || pclen == (size_t)-1 ||
191 return (FNM_NOMATCH);
200 else if ((flags & FNM_CASEFOLD) &&
201 (towlower(pc) == towlower(sc)))
206 * If we have a mismatch (other than hitting
207 * the end of the string), go back to the last
208 * '*' seen and have it match one additional
211 if (bt_pattern == NULL)
212 return (FNM_NOMATCH);
213 sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX,
215 if (sclen == (size_t)-1 ||
216 sclen == (size_t)-2) {
217 sc = (unsigned char)*bt_string;
219 memset(&bt_strmbs, 0,
223 return (FNM_NOMATCH);
224 if (sc == '/' && flags & FNM_PATHNAME)
225 return (FNM_NOMATCH);
227 pattern = bt_pattern, patmbs = bt_patmbs;
228 string = bt_string, strmbs = bt_strmbs;
237 rangematch(const char *pattern, wchar_t test, int flags, char **newp,
244 struct xlocale_collate *table =
245 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
248 * A bracket expression starting with an unquoted circumflex
249 * character produces unspecified results (IEEE 1003.2-1992,
250 * 3.13.2). This implementation treats it like '!', for
251 * consistency with the regular expression syntax.
252 * J.T. Conklin (conklin@ngai.kaleida.com)
254 if ((negate = (*pattern == '!' || *pattern == '^')))
257 if (flags & FNM_CASEFOLD)
258 test = towlower(test);
261 * A right bracket shall lose its special meaning and represent
262 * itself in a bracket expression if it occurs first in the list.
268 if (*pattern == ']' && pattern > origpat) {
271 } else if (*pattern == '\0') {
272 return (RANGE_ERROR);
273 } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
274 return (RANGE_NOMATCH);
275 } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
277 pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
278 if (pclen == (size_t)-1 || pclen == (size_t)-2)
279 return (RANGE_NOMATCH);
282 if (flags & FNM_CASEFOLD)
285 if (*pattern == '-' && *(pattern + 1) != EOS &&
286 *(pattern + 1) != ']') {
287 if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
290 pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
291 if (pclen == (size_t)-1 || pclen == (size_t)-2)
292 return (RANGE_NOMATCH);
295 return (RANGE_ERROR);
297 if (flags & FNM_CASEFOLD)
300 if (table->__collate_load_error ?
301 c <= test && test <= c2 :
302 __wcollate_range_cmp(c, test) <= 0
303 && __wcollate_range_cmp(test, c2) <= 0
306 } else if (c == test)
310 *newp = (char *)pattern;
311 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);