1331722Seadler/*
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/11/lib/libc/gen/fnmatch.c 323548 2017-09-13 16:21:11Z 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
79288029Srodrigcfnmatch(const char *pattern, const char *string, int flags)
801573Srgrimes{
81132812Stjr	static const mbstate_t initial;
82132812Stjr
83206711Sjilles	return (fnmatch1(pattern, string, string, flags, initial, initial));
84132812Stjr}
85132812Stjr
86132812Stjrstatic int
87288029Srodrigcfnmatch1(const char *pattern, const char *string, const char *stringstart,
88288029Srodrigc    int flags, mbstate_t patmbs, mbstate_t strmbs)
89132812Stjr{
90288309Sjilles	const char *bt_pattern, *bt_string;
91288309Sjilles	mbstate_t bt_patmbs, bt_strmbs;
9226484Sache	char *newp;
93132812Stjr	char c;
94132812Stjr	wchar_t pc, sc;
95132812Stjr	size_t pclen, sclen;
961573Srgrimes
97288309Sjilles	bt_pattern = bt_string = NULL;
98206711Sjilles	for (;;) {
99132812Stjr		pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs);
100132812Stjr		if (pclen == (size_t)-1 || pclen == (size_t)-2)
101132812Stjr			return (FNM_NOMATCH);
102132812Stjr		pattern += pclen;
103132812Stjr		sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
104132812Stjr		if (sclen == (size_t)-1 || sclen == (size_t)-2) {
105132812Stjr			sc = (unsigned char)*string;
106132812Stjr			sclen = 1;
107132812Stjr			memset(&strmbs, 0, sizeof(strmbs));
108132812Stjr		}
109132812Stjr		switch (pc) {
1101573Srgrimes		case EOS:
111132812Stjr			if ((flags & FNM_LEADING_DIR) && sc == '/')
11219132Sache				return (0);
113288309Sjilles			if (sc == EOS)
114288309Sjilles				return (0);
115288309Sjilles			goto backtrack;
1161573Srgrimes		case '?':
117132812Stjr			if (sc == EOS)
1181573Srgrimes				return (FNM_NOMATCH);
119132812Stjr			if (sc == '/' && (flags & FNM_PATHNAME))
120288309Sjilles				goto backtrack;
121132812Stjr			if (sc == '.' && (flags & FNM_PERIOD) &&
1221573Srgrimes			    (string == stringstart ||
1231573Srgrimes			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
124288309Sjilles				goto backtrack;
125132812Stjr			string += sclen;
1261573Srgrimes			break;
1271573Srgrimes		case '*':
1281573Srgrimes			c = *pattern;
1291573Srgrimes			/* Collapse multiple stars. */
1301573Srgrimes			while (c == '*')
1311573Srgrimes				c = *++pattern;
1321573Srgrimes
133132812Stjr			if (sc == '.' && (flags & FNM_PERIOD) &&
1341573Srgrimes			    (string == stringstart ||
1351573Srgrimes			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
136288309Sjilles				goto backtrack;
1371573Srgrimes
1381573Srgrimes			/* Optimize for pattern with * at end or before /. */
1391573Srgrimes			if (c == EOS)
1401573Srgrimes				if (flags & FNM_PATHNAME)
14125269Sjdp					return ((flags & FNM_LEADING_DIR) ||
14225269Sjdp					    strchr(string, '/') == NULL ?
1431573Srgrimes					    0 : FNM_NOMATCH);
1441573Srgrimes				else
1451573Srgrimes					return (0);
1461573Srgrimes			else if (c == '/' && flags & FNM_PATHNAME) {
1471573Srgrimes				if ((string = strchr(string, '/')) == NULL)
1481573Srgrimes					return (FNM_NOMATCH);
1491573Srgrimes				break;
1501573Srgrimes			}
1511573Srgrimes
152288309Sjilles			/*
153288309Sjilles			 * First try the shortest match for the '*' that
154288309Sjilles			 * could work. We can forget any earlier '*' since
155288309Sjilles			 * there is no way having it match more characters
156288309Sjilles			 * can help us, given that we are already here.
157288309Sjilles			 */
158288309Sjilles			bt_pattern = pattern, bt_patmbs = patmbs;
159288309Sjilles			bt_string = string, bt_strmbs = strmbs;
160288309Sjilles			break;
1611573Srgrimes		case '[':
162132812Stjr			if (sc == EOS)
1631573Srgrimes				return (FNM_NOMATCH);
164132812Stjr			if (sc == '/' && (flags & FNM_PATHNAME))
165288309Sjilles				goto backtrack;
166132812Stjr			if (sc == '.' && (flags & FNM_PERIOD) &&
16726486Sache			    (string == stringstart ||
16826486Sache			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
169288309Sjilles				goto backtrack;
17026486Sache
171132812Stjr			switch (rangematch(pattern, sc, flags, &newp,
172132812Stjr			    &patmbs)) {
17326484Sache			case RANGE_ERROR:
17426484Sache				goto norm;
17526484Sache			case RANGE_MATCH:
17626484Sache				pattern = newp;
17726484Sache				break;
17826484Sache			case RANGE_NOMATCH:
179288309Sjilles				goto backtrack;
18026484Sache			}
181132812Stjr			string += sclen;
1821573Srgrimes			break;
1831573Srgrimes		case '\\':
1841573Srgrimes			if (!(flags & FNM_NOESCAPE)) {
185132812Stjr				pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
186132812Stjr				    &patmbs);
187322524Spfg				if (pclen == 0 || pclen == (size_t)-1 ||
188322524Spfg				    pclen == (size_t)-2)
189132812Stjr					return (FNM_NOMATCH);
190132812Stjr				pattern += pclen;
1911573Srgrimes			}
1921573Srgrimes			/* FALLTHROUGH */
1931573Srgrimes		default:
19426484Sache		norm:
195288309Sjilles			string += sclen;
196132812Stjr			if (pc == sc)
19719059Swosch				;
19819132Sache			else if ((flags & FNM_CASEFOLD) &&
199132812Stjr				 (towlower(pc) == towlower(sc)))
20019059Swosch				;
201288309Sjilles			else {
202288309Sjilles		backtrack:
203288309Sjilles				/*
204288309Sjilles				 * If we have a mismatch (other than hitting
205288309Sjilles				 * the end of the string), go back to the last
206288309Sjilles				 * '*' seen and have it match one additional
207288309Sjilles				 * character.
208288309Sjilles				 */
209288309Sjilles				if (bt_pattern == NULL)
210288309Sjilles					return (FNM_NOMATCH);
211288309Sjilles				sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX,
212288309Sjilles				    &bt_strmbs);
213288309Sjilles				if (sclen == (size_t)-1 ||
214288309Sjilles				    sclen == (size_t)-2) {
215288309Sjilles					sc = (unsigned char)*bt_string;
216288309Sjilles					sclen = 1;
217288309Sjilles					memset(&bt_strmbs, 0,
218288309Sjilles					    sizeof(bt_strmbs));
219288309Sjilles				}
220288309Sjilles				if (sc == EOS)
221288309Sjilles					return (FNM_NOMATCH);
222288309Sjilles				if (sc == '/' && flags & FNM_PATHNAME)
223288309Sjilles					return (FNM_NOMATCH);
224288309Sjilles				bt_string += sclen;
225288309Sjilles				pattern = bt_pattern, patmbs = bt_patmbs;
226288309Sjilles				string = bt_string, strmbs = bt_strmbs;
227288309Sjilles			}
2281573Srgrimes			break;
2291573Srgrimes		}
230132812Stjr	}
2311573Srgrimes	/* NOTREACHED */
2321573Srgrimes}
2331573Srgrimes
23426484Sachestatic int
235288029Srodrigcrangematch(const char *pattern, wchar_t test, int flags, char **newp,
236288029Srodrigc    mbstate_t *patmbs)
2371573Srgrimes{
23826492Sache	int negate, ok;
239132812Stjr	wchar_t c, c2;
240132812Stjr	size_t pclen;
241132812Stjr	const char *origpat;
242227753Stheraven	struct xlocale_collate *table =
243227753Stheraven		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
2441573Srgrimes
2451573Srgrimes	/*
2461573Srgrimes	 * A bracket expression starting with an unquoted circumflex
2471573Srgrimes	 * character produces unspecified results (IEEE 1003.2-1992,
2481573Srgrimes	 * 3.13.2).  This implementation treats it like '!', for
2491573Srgrimes	 * consistency with the regular expression syntax.
2501573Srgrimes	 * J.T. Conklin (conklin@ngai.kaleida.com)
2511573Srgrimes	 */
252323548Spfg	if ((negate = (*pattern == '!' || *pattern == '^')))
2531573Srgrimes		++pattern;
2548870Srgrimes
25519132Sache	if (flags & FNM_CASEFOLD)
256132812Stjr		test = towlower(test);
25719059Swosch
25826484Sache	/*
25926484Sache	 * A right bracket shall lose its special meaning and represent
26026484Sache	 * itself in a bracket expression if it occurs first in the list.
26126484Sache	 * -- POSIX.2 2.8.3.2
26226484Sache	 */
26326492Sache	ok = 0;
264132812Stjr	origpat = pattern;
265132812Stjr	for (;;) {
266132812Stjr		if (*pattern == ']' && pattern > origpat) {
267132812Stjr			pattern++;
268132812Stjr			break;
269132812Stjr		} else if (*pattern == '\0') {
27026484Sache			return (RANGE_ERROR);
271132812Stjr		} else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
27226486Sache			return (RANGE_NOMATCH);
273132812Stjr		} else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
274132812Stjr			pattern++;
275132812Stjr		pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
276132812Stjr		if (pclen == (size_t)-1 || pclen == (size_t)-2)
277132812Stjr			return (RANGE_NOMATCH);
278132812Stjr		pattern += pclen;
27926486Sache
28019132Sache		if (flags & FNM_CASEFOLD)
281132812Stjr			c = towlower(c);
28219059Swosch
283132812Stjr		if (*pattern == '-' && *(pattern + 1) != EOS &&
284132812Stjr		    *(pattern + 1) != ']') {
285132812Stjr			if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
286132812Stjr				if (*pattern != EOS)
287132812Stjr					pattern++;
288132812Stjr			pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
289132812Stjr			if (pclen == (size_t)-1 || pclen == (size_t)-2)
290132812Stjr				return (RANGE_NOMATCH);
291132812Stjr			pattern += pclen;
2921573Srgrimes			if (c2 == EOS)
29326484Sache				return (RANGE_ERROR);
29419059Swosch
29519132Sache			if (flags & FNM_CASEFOLD)
296132812Stjr				c2 = towlower(c2);
29719059Swosch
298227753Stheraven			if (table->__collate_load_error ?
29924632Sache			    c <= test && test <= c2 :
300304275Sache			       __wcollate_range_cmp(c, test) <= 0
301304275Sache			    && __wcollate_range_cmp(test, c2) <= 0
30217533Sache			   )
3031573Srgrimes				ok = 1;
3041573Srgrimes		} else if (c == test)
3051573Srgrimes			ok = 1;
306132812Stjr	}
30726492Sache
30826484Sache	*newp = (char *)pattern;
30926484Sache	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
3101573Srgrimes}
311