11573Srgrimes/*
21573Srgrimes * Copyright (c) 1989, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Guido van Rossum.
71573Srgrimes *
8227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation
9227753Stheraven * All rights reserved.
10227753Stheraven * Portions of this software were developed by David Chisnall
11227753Stheraven * under sponsorship from the FreeBSD Foundation.
12227753Stheraven *
131573Srgrimes * Redistribution and use in source and binary forms, with or without
141573Srgrimes * modification, are permitted provided that the following conditions
151573Srgrimes * are met:
161573Srgrimes * 1. Redistributions of source code must retain the above copyright
171573Srgrimes *    notice, this list of conditions and the following disclaimer.
181573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
191573Srgrimes *    notice, this list of conditions and the following disclaimer in the
201573Srgrimes *    documentation and/or other materials provided with the distribution.
211573Srgrimes * 4. Neither the name of the University nor the names of its contributors
221573Srgrimes *    may be used to endorse or promote products derived from this software
231573Srgrimes *    without specific prior written permission.
241573Srgrimes *
251573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351573Srgrimes * SUCH DAMAGE.
361573Srgrimes */
371573Srgrimes
381573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
391573Srgrimesstatic char sccsid[] = "@(#)fnmatch.c	8.2 (Berkeley) 4/16/94";
401573Srgrimes#endif /* LIBC_SCCS and not lint */
4190045Sobrien#include <sys/cdefs.h>
4290045Sobrien__FBSDID("$FreeBSD: stable/10/lib/libc/gen/fnmatch.c 324505 2017-10-10 21:04:40Z pfg $");
431573Srgrimes
441573Srgrimes/*
451573Srgrimes * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
461573Srgrimes * Compares a filename or pathname to a pattern.
471573Srgrimes */
481573Srgrimes
49132812Stjr/*
50132812Stjr * Some notes on multibyte character support:
51132812Stjr * 1. Patterns with illegal byte sequences match nothing.
52132812Stjr * 2. Illegal byte sequences in the "string" argument are handled by treating
53132812Stjr *    them as single-byte characters with a value of the first byte of the
54132812Stjr *    sequence cast to wchar_t.
55132812Stjr * 3. Multibyte conversion state objects (mbstate_t) are passed around and
56132812Stjr *    used for most, but not all, conversions. Further work will be required
57132812Stjr *    to support state-dependent encodings.
58132812Stjr */
59132812Stjr
601573Srgrimes#include <fnmatch.h>
61132812Stjr#include <limits.h>
621573Srgrimes#include <string.h>
63132812Stjr#include <wchar.h>
64132812Stjr#include <wctype.h>
651573Srgrimes
6619276Sache#include "collate.h"
6719276Sache
681573Srgrimes#define	EOS	'\0'
691573Srgrimes
7026484Sache#define RANGE_MATCH     1
7126484Sache#define RANGE_NOMATCH   0
7226484Sache#define RANGE_ERROR     (-1)
731573Srgrimes
74132812Stjrstatic int rangematch(const char *, wchar_t, int, char **, mbstate_t *);
75206711Sjillesstatic int fnmatch1(const char *, const char *, const char *, int, mbstate_t,
76206711Sjilles		mbstate_t);
7726484Sache
781573Srgrimesint
791573Srgrimesfnmatch(pattern, string, flags)
801573Srgrimes	const char *pattern, *string;
811573Srgrimes	int flags;
821573Srgrimes{
83132812Stjr	static const mbstate_t initial;
84132812Stjr
85206711Sjilles	return (fnmatch1(pattern, string, string, flags, initial, initial));
86132812Stjr}
87132812Stjr
88132812Stjrstatic int
89206711Sjillesfnmatch1(pattern, string, stringstart, flags, patmbs, strmbs)
90206711Sjilles	const char *pattern, *string, *stringstart;
91132812Stjr	int flags;
92132812Stjr	mbstate_t patmbs, strmbs;
93132812Stjr{
94289943Sjilles	const char *bt_pattern, *bt_string;
95289943Sjilles	mbstate_t bt_patmbs, bt_strmbs;
9626484Sache	char *newp;
97132812Stjr	char c;
98132812Stjr	wchar_t pc, sc;
99132812Stjr	size_t pclen, sclen;
1001573Srgrimes
101289943Sjilles	bt_pattern = bt_string = NULL;
102206711Sjilles	for (;;) {
103132812Stjr		pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs);
104132812Stjr		if (pclen == (size_t)-1 || pclen == (size_t)-2)
105132812Stjr			return (FNM_NOMATCH);
106132812Stjr		pattern += pclen;
107132812Stjr		sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
108132812Stjr		if (sclen == (size_t)-1 || sclen == (size_t)-2) {
109132812Stjr			sc = (unsigned char)*string;
110132812Stjr			sclen = 1;
111132812Stjr			memset(&strmbs, 0, sizeof(strmbs));
112132812Stjr		}
113132812Stjr		switch (pc) {
1141573Srgrimes		case EOS:
115132812Stjr			if ((flags & FNM_LEADING_DIR) && sc == '/')
11619132Sache				return (0);
117289943Sjilles			if (sc == EOS)
118289943Sjilles				return (0);
119289943Sjilles			goto backtrack;
1201573Srgrimes		case '?':
121132812Stjr			if (sc == EOS)
1221573Srgrimes				return (FNM_NOMATCH);
123132812Stjr			if (sc == '/' && (flags & FNM_PATHNAME))
124289943Sjilles				goto backtrack;
125132812Stjr			if (sc == '.' && (flags & FNM_PERIOD) &&
1261573Srgrimes			    (string == stringstart ||
1271573Srgrimes			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
128289943Sjilles				goto backtrack;
129132812Stjr			string += sclen;
1301573Srgrimes			break;
1311573Srgrimes		case '*':
1321573Srgrimes			c = *pattern;
1331573Srgrimes			/* Collapse multiple stars. */
1341573Srgrimes			while (c == '*')
1351573Srgrimes				c = *++pattern;
1361573Srgrimes
137132812Stjr			if (sc == '.' && (flags & FNM_PERIOD) &&
1381573Srgrimes			    (string == stringstart ||
1391573Srgrimes			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
140289943Sjilles				goto backtrack;
1411573Srgrimes
1421573Srgrimes			/* Optimize for pattern with * at end or before /. */
1431573Srgrimes			if (c == EOS)
1441573Srgrimes				if (flags & FNM_PATHNAME)
14525269Sjdp					return ((flags & FNM_LEADING_DIR) ||
14625269Sjdp					    strchr(string, '/') == NULL ?
1471573Srgrimes					    0 : FNM_NOMATCH);
1481573Srgrimes				else
1491573Srgrimes					return (0);
1501573Srgrimes			else if (c == '/' && flags & FNM_PATHNAME) {
1511573Srgrimes				if ((string = strchr(string, '/')) == NULL)
1521573Srgrimes					return (FNM_NOMATCH);
1531573Srgrimes				break;
1541573Srgrimes			}
1551573Srgrimes
156289943Sjilles			/*
157289943Sjilles			 * First try the shortest match for the '*' that
158289943Sjilles			 * could work. We can forget any earlier '*' since
159289943Sjilles			 * there is no way having it match more characters
160289943Sjilles			 * can help us, given that we are already here.
161289943Sjilles			 */
162289943Sjilles			bt_pattern = pattern, bt_patmbs = patmbs;
163289943Sjilles			bt_string = string, bt_strmbs = strmbs;
164289943Sjilles			break;
1651573Srgrimes		case '[':
166132812Stjr			if (sc == EOS)
1671573Srgrimes				return (FNM_NOMATCH);
168132812Stjr			if (sc == '/' && (flags & FNM_PATHNAME))
169289943Sjilles				goto backtrack;
170132812Stjr			if (sc == '.' && (flags & FNM_PERIOD) &&
17126486Sache			    (string == stringstart ||
17226486Sache			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
173289943Sjilles				goto backtrack;
17426486Sache
175132812Stjr			switch (rangematch(pattern, sc, flags, &newp,
176132812Stjr			    &patmbs)) {
17726484Sache			case RANGE_ERROR:
17826484Sache				goto norm;
17926484Sache			case RANGE_MATCH:
18026484Sache				pattern = newp;
18126484Sache				break;
18226484Sache			case RANGE_NOMATCH:
183289943Sjilles				goto backtrack;
18426484Sache			}
185132812Stjr			string += sclen;
1861573Srgrimes			break;
1871573Srgrimes		case '\\':
1881573Srgrimes			if (!(flags & FNM_NOESCAPE)) {
189132812Stjr				pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
190132812Stjr				    &patmbs);
191324505Spfg				if (pclen == 0 || pclen == (size_t)-1 ||
192324505Spfg				    pclen == (size_t)-2)
193132812Stjr					return (FNM_NOMATCH);
194132812Stjr				pattern += pclen;
1951573Srgrimes			}
1961573Srgrimes			/* FALLTHROUGH */
1971573Srgrimes		default:
19826484Sache		norm:
199289943Sjilles			string += sclen;
200132812Stjr			if (pc == sc)
20119059Swosch				;
20219132Sache			else if ((flags & FNM_CASEFOLD) &&
203132812Stjr				 (towlower(pc) == towlower(sc)))
20419059Swosch				;
205289943Sjilles			else {
206289943Sjilles		backtrack:
207289943Sjilles				/*
208289943Sjilles				 * If we have a mismatch (other than hitting
209289943Sjilles				 * the end of the string), go back to the last
210289943Sjilles				 * '*' seen and have it match one additional
211289943Sjilles				 * character.
212289943Sjilles				 */
213289943Sjilles				if (bt_pattern == NULL)
214289943Sjilles					return (FNM_NOMATCH);
215289943Sjilles				sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX,
216289943Sjilles				    &bt_strmbs);
217289943Sjilles				if (sclen == (size_t)-1 ||
218289943Sjilles				    sclen == (size_t)-2) {
219289943Sjilles					sc = (unsigned char)*bt_string;
220289943Sjilles					sclen = 1;
221289943Sjilles					memset(&bt_strmbs, 0,
222289943Sjilles					    sizeof(bt_strmbs));
223289943Sjilles				}
224289943Sjilles				if (sc == EOS)
225289943Sjilles					return (FNM_NOMATCH);
226289943Sjilles				if (sc == '/' && flags & FNM_PATHNAME)
227289943Sjilles					return (FNM_NOMATCH);
228289943Sjilles				bt_string += sclen;
229289943Sjilles				pattern = bt_pattern, patmbs = bt_patmbs;
230289943Sjilles				string = bt_string, strmbs = bt_strmbs;
231289943Sjilles			}
2321573Srgrimes			break;
2331573Srgrimes		}
234132812Stjr	}
2351573Srgrimes	/* NOTREACHED */
2361573Srgrimes}
2371573Srgrimes
23826484Sachestatic int
239132812Stjrrangematch(pattern, test, flags, newp, patmbs)
2401573Srgrimes	const char *pattern;
241132812Stjr	wchar_t test;
24219132Sache	int flags;
24326484Sache	char **newp;
244132812Stjr	mbstate_t *patmbs;
2451573Srgrimes{
24626492Sache	int negate, ok;
247132812Stjr	wchar_t c, c2;
248132812Stjr	size_t pclen;
249132812Stjr	const char *origpat;
250227753Stheraven	struct xlocale_collate *table =
251227753Stheraven		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
2521573Srgrimes
2531573Srgrimes	/*
2541573Srgrimes	 * A bracket expression starting with an unquoted circumflex
2551573Srgrimes	 * character produces unspecified results (IEEE 1003.2-1992,
2561573Srgrimes	 * 3.13.2).  This implementation treats it like '!', for
2571573Srgrimes	 * consistency with the regular expression syntax.
2581573Srgrimes	 * J.T. Conklin (conklin@ngai.kaleida.com)
2591573Srgrimes	 */
26026486Sache	if ( (negate = (*pattern == '!' || *pattern == '^')) )
2611573Srgrimes		++pattern;
2628870Srgrimes
26319132Sache	if (flags & FNM_CASEFOLD)
264132812Stjr		test = towlower(test);
26519059Swosch
26626484Sache	/*
26726484Sache	 * A right bracket shall lose its special meaning and represent
26826484Sache	 * itself in a bracket expression if it occurs first in the list.
26926484Sache	 * -- POSIX.2 2.8.3.2
27026484Sache	 */
27126492Sache	ok = 0;
272132812Stjr	origpat = pattern;
273132812Stjr	for (;;) {
274132812Stjr		if (*pattern == ']' && pattern > origpat) {
275132812Stjr			pattern++;
276132812Stjr			break;
277132812Stjr		} else if (*pattern == '\0') {
27826484Sache			return (RANGE_ERROR);
279132812Stjr		} else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
28026486Sache			return (RANGE_NOMATCH);
281132812Stjr		} else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
282132812Stjr			pattern++;
283132812Stjr		pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
284132812Stjr		if (pclen == (size_t)-1 || pclen == (size_t)-2)
285132812Stjr			return (RANGE_NOMATCH);
286132812Stjr		pattern += pclen;
28726486Sache
28819132Sache		if (flags & FNM_CASEFOLD)
289132812Stjr			c = towlower(c);
29019059Swosch
291132812Stjr		if (*pattern == '-' && *(pattern + 1) != EOS &&
292132812Stjr		    *(pattern + 1) != ']') {
293132812Stjr			if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
294132812Stjr				if (*pattern != EOS)
295132812Stjr					pattern++;
296132812Stjr			pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
297132812Stjr			if (pclen == (size_t)-1 || pclen == (size_t)-2)
298132812Stjr				return (RANGE_NOMATCH);
299132812Stjr			pattern += pclen;
3001573Srgrimes			if (c2 == EOS)
30126484Sache				return (RANGE_ERROR);
30219059Swosch
30319132Sache			if (flags & FNM_CASEFOLD)
304132812Stjr				c2 = towlower(c2);
30519059Swosch
306227753Stheraven			if (table->__collate_load_error ?
30724632Sache			    c <= test && test <= c2 :
308303185Sache			       __wcollate_range_cmp(c, test) <= 0
309303185Sache			    && __wcollate_range_cmp(test, c2) <= 0
31017533Sache			   )
3111573Srgrimes				ok = 1;
3121573Srgrimes		} else if (c == test)
3131573Srgrimes			ok = 1;
314132812Stjr	}
31526492Sache
31626484Sache	*newp = (char *)pattern;
31726484Sache	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
3181573Srgrimes}
319