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
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
44 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
45 * Compares a filename or pathname to a pattern.
49 * Some notes on multibyte character support:
50 * 1. Patterns with illegal byte sequences match nothing.
51 * 2. Illegal byte sequences in the "string" argument are handled by treating
52 * them as single-byte characters with a value of the first byte of the
53 * sequence cast to wchar_t.
54 * 3. Multibyte conversion state objects (mbstate_t) are passed around and
55 * used for most, but not all, conversions. Further work will be required
56 * to support state-dependent encodings.
70 #define RANGE_NOMATCH 0
71 #define RANGE_ERROR (-1)
73 static int rangematch(const char *, wchar_t, int, char **, mbstate_t *);
74 static int fnmatch1(const char *, const char *, int, mbstate_t, mbstate_t);
77 fnmatch(pattern, string, flags)
78 const char *pattern, *string;
81 static const mbstate_t initial;
83 return (fnmatch1(pattern, string, flags, initial, initial));
87 fnmatch1(pattern, string, flags, patmbs, strmbs)
88 const char *pattern, *string;
90 mbstate_t patmbs, strmbs;
92 const char *stringstart;
98 for (stringstart = string;;) {
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 == '/')
113 return (sc == EOS ? 0 : FNM_NOMATCH);
116 return (FNM_NOMATCH);
117 if (sc == '/' && (flags & FNM_PATHNAME))
118 return (FNM_NOMATCH);
119 if (sc == '.' && (flags & FNM_PERIOD) &&
120 (string == stringstart ||
121 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
122 return (FNM_NOMATCH);
127 /* Collapse multiple stars. */
131 if (sc == '.' && (flags & FNM_PERIOD) &&
132 (string == stringstart ||
133 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
134 return (FNM_NOMATCH);
136 /* Optimize for pattern with * at end or before /. */
138 if (flags & FNM_PATHNAME)
139 return ((flags & FNM_LEADING_DIR) ||
140 strchr(string, '/') == NULL ?
144 else if (c == '/' && flags & FNM_PATHNAME) {
145 if ((string = strchr(string, '/')) == NULL)
146 return (FNM_NOMATCH);
150 /* General case, use recursion. */
152 if (!fnmatch1(pattern, string,
153 flags & ~FNM_PERIOD, patmbs, strmbs))
155 sclen = mbrtowc(&sc, string, MB_LEN_MAX,
157 if (sclen == (size_t)-1 ||
158 sclen == (size_t)-2) {
159 sc = (unsigned char)*string;
161 memset(&strmbs, 0, sizeof(strmbs));
163 if (sc == '/' && flags & FNM_PATHNAME)
167 return (FNM_NOMATCH);
170 return (FNM_NOMATCH);
171 if (sc == '/' && (flags & FNM_PATHNAME))
172 return (FNM_NOMATCH);
173 if (sc == '.' && (flags & FNM_PERIOD) &&
174 (string == stringstart ||
175 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
176 return (FNM_NOMATCH);
178 switch (rangematch(pattern, sc, flags, &newp,
186 return (FNM_NOMATCH);
191 if (!(flags & FNM_NOESCAPE)) {
192 pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
194 if (pclen == (size_t)-1 || pclen == (size_t)-2)
195 return (FNM_NOMATCH);
205 else if ((flags & FNM_CASEFOLD) &&
206 (towlower(pc) == towlower(sc)))
209 return (FNM_NOMATCH);
218 rangematch(pattern, test, flags, newp, patmbs)
231 * A bracket expression starting with an unquoted circumflex
232 * character produces unspecified results (IEEE 1003.2-1992,
233 * 3.13.2). This implementation treats it like '!', for
234 * consistency with the regular expression syntax.
235 * J.T. Conklin (conklin@ngai.kaleida.com)
237 if ( (negate = (*pattern == '!' || *pattern == '^')) )
240 if (flags & FNM_CASEFOLD)
241 test = towlower(test);
244 * A right bracket shall lose its special meaning and represent
245 * itself in a bracket expression if it occurs first in the list.
251 if (*pattern == ']' && pattern > origpat) {
254 } else if (*pattern == '\0') {
255 return (RANGE_ERROR);
256 } else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
257 return (RANGE_NOMATCH);
258 } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
260 pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
261 if (pclen == (size_t)-1 || pclen == (size_t)-2)
262 return (RANGE_NOMATCH);
265 if (flags & FNM_CASEFOLD)
268 if (*pattern == '-' && *(pattern + 1) != EOS &&
269 *(pattern + 1) != ']') {
270 if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
273 pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
274 if (pclen == (size_t)-1 || pclen == (size_t)-2)
275 return (RANGE_NOMATCH);
278 return (RANGE_ERROR);
280 if (flags & FNM_CASEFOLD)
283 if (__collate_load_error ?
284 c <= test && test <= c2 :
285 __collate_range_cmp(c, test) <= 0
286 && __collate_range_cmp(test, c2) <= 0
289 } else if (c == test)
293 *newp = (char *)pattern;
294 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);