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 #include <sys/cdefs.h>
41 __SCCSID("@(#)fnmatch.c 8.2 (Berkeley) 4/16/94");
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(const char *pattern, const char *string, int flags)
81 static const mbstate_t initial;
83 return (fnmatch1(pattern, string, string, flags, initial, initial));
87 fnmatch1(const char *pattern, const char *string, const char *stringstart,
88 int flags, mbstate_t patmbs, mbstate_t strmbs)
90 const char *bt_pattern, *bt_string;
91 mbstate_t bt_patmbs, bt_strmbs;
97 bt_pattern = bt_string = NULL;
99 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs);
100 if (pclen == (size_t)-1 || pclen == (size_t)-2)
101 return (FNM_NOMATCH);
103 sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
104 if (sclen == (size_t)-1 || sclen == (size_t)-2) {
105 sc = (unsigned char)*string;
107 memset(&strmbs, 0, sizeof(strmbs));
111 if ((flags & FNM_LEADING_DIR) && sc == '/')
118 return (FNM_NOMATCH);
119 if (sc == '/' && (flags & FNM_PATHNAME))
121 if (sc == '.' && (flags & FNM_PERIOD) &&
122 (string == stringstart ||
123 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
129 /* Collapse multiple stars. */
133 if (sc == '.' && (flags & FNM_PERIOD) &&
134 (string == stringstart ||
135 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
138 /* Optimize for pattern with * at end or before /. */
140 if (flags & FNM_PATHNAME)
141 return ((flags & FNM_LEADING_DIR) ||
142 strchr(string, '/') == NULL ?
146 else if (c == '/' && flags & FNM_PATHNAME) {
147 if ((string = strchr(string, '/')) == NULL)
148 return (FNM_NOMATCH);
153 * First try the shortest match for the '*' that
154 * could work. We can forget any earlier '*' since
155 * there is no way having it match more characters
156 * can help us, given that we are already here.
158 bt_pattern = pattern, bt_patmbs = patmbs;
159 bt_string = string, bt_strmbs = strmbs;
163 return (FNM_NOMATCH);
164 if (sc == '/' && (flags & FNM_PATHNAME))
166 if (sc == '.' && (flags & FNM_PERIOD) &&
167 (string == stringstart ||
168 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
171 switch (rangematch(pattern, sc, flags, &newp,
184 if (!(flags & FNM_NOESCAPE)) {
185 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
187 if (pclen == 0 || pclen == (size_t)-1 ||
189 return (FNM_NOMATCH);
198 else if ((flags & FNM_CASEFOLD) &&
199 (towlower(pc) == towlower(sc)))
204 * If we have a mismatch (other than hitting
205 * the end of the string), go back to the last
206 * '*' seen and have it match one additional
209 if (bt_pattern == NULL)
210 return (FNM_NOMATCH);
211 sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX,
213 if (sclen == (size_t)-1 ||
214 sclen == (size_t)-2) {
215 sc = (unsigned char)*bt_string;
217 memset(&bt_strmbs, 0,
221 return (FNM_NOMATCH);
222 if (sc == '/' && flags & FNM_PATHNAME)
223 return (FNM_NOMATCH);
225 pattern = bt_pattern, patmbs = bt_patmbs;
226 string = bt_string, strmbs = bt_strmbs;
235 rangematch(const char *pattern, wchar_t test, int flags, char **newp,
242 struct xlocale_collate *table =
243 (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
246 * A bracket expression starting with an unquoted circumflex
247 * character produces unspecified results (IEEE 1003.2-1992,
248 * 3.13.2). This implementation treats it like '!', for
249 * consistency with the regular expression syntax.
250 * J.T. Conklin (conklin@ngai.kaleida.com)
252 if ((negate = (*pattern == '!' || *pattern == '^')))
255 if (flags & FNM_CASEFOLD)
256 test = towlower(test);
259 * A right bracket shall lose its special meaning and represent
260 * itself in a bracket expression if it occurs first in the list.
266 if (*pattern == ']' && pattern > origpat) {
269 } else if (*pattern == '\0') {
270 return (RANGE_ERROR);
271 } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
272 return (RANGE_NOMATCH);
273 } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
275 pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
276 if (pclen == (size_t)-1 || pclen == (size_t)-2)
277 return (RANGE_NOMATCH);
280 if (flags & FNM_CASEFOLD)
283 if (*pattern == '-' && *(pattern + 1) != EOS &&
284 *(pattern + 1) != ']') {
285 if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
288 pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
289 if (pclen == (size_t)-1 || pclen == (size_t)-2)
290 return (RANGE_NOMATCH);
293 return (RANGE_ERROR);
295 if (flags & FNM_CASEFOLD)
298 if (table->__collate_load_error ?
299 c <= test && test <= c2 :
300 __wcollate_range_cmp(c, test) <= 0
301 && __wcollate_range_cmp(test, c2) <= 0
304 } else if (c == test)
308 *newp = (char *)pattern;
309 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);