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: releng/10.3/lib/libc/gen/fnmatch.c 289943 2015-10-25 21:39:23Z jilles $");
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);
191132812Stjr				if (pclen == (size_t)-1 || pclen == (size_t)-2)
192132812Stjr					return (FNM_NOMATCH);
193132812Stjr				pattern += pclen;
1941573Srgrimes			}
1951573Srgrimes			/* FALLTHROUGH */
1961573Srgrimes		default:
19726484Sache		norm:
198289943Sjilles			string += sclen;
199132812Stjr			if (pc == sc)
20019059Swosch				;
20119132Sache			else if ((flags & FNM_CASEFOLD) &&
202132812Stjr				 (towlower(pc) == towlower(sc)))
20319059Swosch				;
204289943Sjilles			else {
205289943Sjilles		backtrack:
206289943Sjilles				/*
207289943Sjilles				 * If we have a mismatch (other than hitting
208289943Sjilles				 * the end of the string), go back to the last
209289943Sjilles				 * '*' seen and have it match one additional
210289943Sjilles				 * character.
211289943Sjilles				 */
212289943Sjilles				if (bt_pattern == NULL)
213289943Sjilles					return (FNM_NOMATCH);
214289943Sjilles				sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX,
215289943Sjilles				    &bt_strmbs);
216289943Sjilles				if (sclen == (size_t)-1 ||
217289943Sjilles				    sclen == (size_t)-2) {
218289943Sjilles					sc = (unsigned char)*bt_string;
219289943Sjilles					sclen = 1;
220289943Sjilles					memset(&bt_strmbs, 0,
221289943Sjilles					    sizeof(bt_strmbs));
222289943Sjilles				}
223289943Sjilles				if (sc == EOS)
224289943Sjilles					return (FNM_NOMATCH);
225289943Sjilles				if (sc == '/' && flags & FNM_PATHNAME)
226289943Sjilles					return (FNM_NOMATCH);
227289943Sjilles				bt_string += sclen;
228289943Sjilles				pattern = bt_pattern, patmbs = bt_patmbs;
229289943Sjilles				string = bt_string, strmbs = bt_strmbs;
230289943Sjilles			}
2311573Srgrimes			break;
2321573Srgrimes		}
233132812Stjr	}
2341573Srgrimes	/* NOTREACHED */
2351573Srgrimes}
2361573Srgrimes
23726484Sachestatic int
238132812Stjrrangematch(pattern, test, flags, newp, patmbs)
2391573Srgrimes	const char *pattern;
240132812Stjr	wchar_t test;
24119132Sache	int flags;
24226484Sache	char **newp;
243132812Stjr	mbstate_t *patmbs;
2441573Srgrimes{
24526492Sache	int negate, ok;
246132812Stjr	wchar_t c, c2;
247132812Stjr	size_t pclen;
248132812Stjr	const char *origpat;
249227753Stheraven	struct xlocale_collate *table =
250227753Stheraven		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
2511573Srgrimes
2521573Srgrimes	/*
2531573Srgrimes	 * A bracket expression starting with an unquoted circumflex
2541573Srgrimes	 * character produces unspecified results (IEEE 1003.2-1992,
2551573Srgrimes	 * 3.13.2).  This implementation treats it like '!', for
2561573Srgrimes	 * consistency with the regular expression syntax.
2571573Srgrimes	 * J.T. Conklin (conklin@ngai.kaleida.com)
2581573Srgrimes	 */
25926486Sache	if ( (negate = (*pattern == '!' || *pattern == '^')) )
2601573Srgrimes		++pattern;
2618870Srgrimes
26219132Sache	if (flags & FNM_CASEFOLD)
263132812Stjr		test = towlower(test);
26419059Swosch
26526484Sache	/*
26626484Sache	 * A right bracket shall lose its special meaning and represent
26726484Sache	 * itself in a bracket expression if it occurs first in the list.
26826484Sache	 * -- POSIX.2 2.8.3.2
26926484Sache	 */
27026492Sache	ok = 0;
271132812Stjr	origpat = pattern;
272132812Stjr	for (;;) {
273132812Stjr		if (*pattern == ']' && pattern > origpat) {
274132812Stjr			pattern++;
275132812Stjr			break;
276132812Stjr		} else if (*pattern == '\0') {
27726484Sache			return (RANGE_ERROR);
278132812Stjr		} else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
27926486Sache			return (RANGE_NOMATCH);
280132812Stjr		} else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
281132812Stjr			pattern++;
282132812Stjr		pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
283132812Stjr		if (pclen == (size_t)-1 || pclen == (size_t)-2)
284132812Stjr			return (RANGE_NOMATCH);
285132812Stjr		pattern += pclen;
28626486Sache
28719132Sache		if (flags & FNM_CASEFOLD)
288132812Stjr			c = towlower(c);
28919059Swosch
290132812Stjr		if (*pattern == '-' && *(pattern + 1) != EOS &&
291132812Stjr		    *(pattern + 1) != ']') {
292132812Stjr			if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
293132812Stjr				if (*pattern != EOS)
294132812Stjr					pattern++;
295132812Stjr			pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
296132812Stjr			if (pclen == (size_t)-1 || pclen == (size_t)-2)
297132812Stjr				return (RANGE_NOMATCH);
298132812Stjr			pattern += pclen;
2991573Srgrimes			if (c2 == EOS)
30026484Sache				return (RANGE_ERROR);
30119059Swosch
30219132Sache			if (flags & FNM_CASEFOLD)
303132812Stjr				c2 = towlower(c2);
30419059Swosch
305227753Stheraven			if (table->__collate_load_error ?
30624632Sache			    c <= test && test <= c2 :
307227753Stheraven			       __collate_range_cmp(table, c, test) <= 0
308227753Stheraven			    && __collate_range_cmp(table, test, c2) <= 0
30917533Sache			   )
3101573Srgrimes				ok = 1;
3111573Srgrimes		} else if (c == test)
3121573Srgrimes			ok = 1;
313132812Stjr	}
31426492Sache
31526484Sache	*newp = (char *)pattern;
31626484Sache	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
3171573Srgrimes}
318