xref: /freebsd/lib/libc/gen/fnmatch.c (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*
2  * Copyright (c) 1989, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Guido van Rossum.
7  *
8  * Copyright (c) 2011 The FreeBSD Foundation
9  * All rights reserved.
10  * Portions of this software were developed by David Chisnall
11  * under sponsorship from the FreeBSD Foundation.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)fnmatch.c	8.2 (Berkeley) 4/16/94";
40 #endif /* LIBC_SCCS and not lint */
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 /*
45  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
46  * Compares a filename or pathname to a pattern.
47  */
48 
49 /*
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.
58  */
59 
60 #include <fnmatch.h>
61 #include <limits.h>
62 #include <string.h>
63 #include <wchar.h>
64 #include <wctype.h>
65 
66 #include "collate.h"
67 
68 #define	EOS	'\0'
69 
70 #define RANGE_MATCH     1
71 #define RANGE_NOMATCH   0
72 #define RANGE_ERROR     (-1)
73 
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,
76 		mbstate_t);
77 
78 int
79 fnmatch(pattern, string, flags)
80 	const char *pattern, *string;
81 	int flags;
82 {
83 	static const mbstate_t initial;
84 
85 	return (fnmatch1(pattern, string, string, flags, initial, initial));
86 }
87 
88 static int
89 fnmatch1(pattern, string, stringstart, flags, patmbs, strmbs)
90 	const char *pattern, *string, *stringstart;
91 	int flags;
92 	mbstate_t patmbs, strmbs;
93 {
94 	char *newp;
95 	char c;
96 	wchar_t pc, sc;
97 	size_t pclen, sclen;
98 
99 	for (;;) {
100 		pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs);
101 		if (pclen == (size_t)-1 || pclen == (size_t)-2)
102 			return (FNM_NOMATCH);
103 		pattern += pclen;
104 		sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
105 		if (sclen == (size_t)-1 || sclen == (size_t)-2) {
106 			sc = (unsigned char)*string;
107 			sclen = 1;
108 			memset(&strmbs, 0, sizeof(strmbs));
109 		}
110 		switch (pc) {
111 		case EOS:
112 			if ((flags & FNM_LEADING_DIR) && sc == '/')
113 				return (0);
114 			return (sc == EOS ? 0 : FNM_NOMATCH);
115 		case '?':
116 			if (sc == EOS)
117 				return (FNM_NOMATCH);
118 			if (sc == '/' && (flags & FNM_PATHNAME))
119 				return (FNM_NOMATCH);
120 			if (sc == '.' && (flags & FNM_PERIOD) &&
121 			    (string == stringstart ||
122 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
123 				return (FNM_NOMATCH);
124 			string += sclen;
125 			break;
126 		case '*':
127 			c = *pattern;
128 			/* Collapse multiple stars. */
129 			while (c == '*')
130 				c = *++pattern;
131 
132 			if (sc == '.' && (flags & FNM_PERIOD) &&
133 			    (string == stringstart ||
134 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
135 				return (FNM_NOMATCH);
136 
137 			/* Optimize for pattern with * at end or before /. */
138 			if (c == EOS)
139 				if (flags & FNM_PATHNAME)
140 					return ((flags & FNM_LEADING_DIR) ||
141 					    strchr(string, '/') == NULL ?
142 					    0 : FNM_NOMATCH);
143 				else
144 					return (0);
145 			else if (c == '/' && flags & FNM_PATHNAME) {
146 				if ((string = strchr(string, '/')) == NULL)
147 					return (FNM_NOMATCH);
148 				break;
149 			}
150 
151 			/* General case, use recursion. */
152 			while (sc != EOS) {
153 				if (!fnmatch1(pattern, string, stringstart,
154 				    flags, patmbs, strmbs))
155 					return (0);
156 				sclen = mbrtowc(&sc, string, MB_LEN_MAX,
157 				    &strmbs);
158 				if (sclen == (size_t)-1 ||
159 				    sclen == (size_t)-2) {
160 					sc = (unsigned char)*string;
161 					sclen = 1;
162 					memset(&strmbs, 0, sizeof(strmbs));
163 				}
164 				if (sc == '/' && flags & FNM_PATHNAME)
165 					break;
166 				string += sclen;
167 			}
168 			return (FNM_NOMATCH);
169 		case '[':
170 			if (sc == EOS)
171 				return (FNM_NOMATCH);
172 			if (sc == '/' && (flags & FNM_PATHNAME))
173 				return (FNM_NOMATCH);
174 			if (sc == '.' && (flags & FNM_PERIOD) &&
175 			    (string == stringstart ||
176 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
177 				return (FNM_NOMATCH);
178 
179 			switch (rangematch(pattern, sc, flags, &newp,
180 			    &patmbs)) {
181 			case RANGE_ERROR:
182 				goto norm;
183 			case RANGE_MATCH:
184 				pattern = newp;
185 				break;
186 			case RANGE_NOMATCH:
187 				return (FNM_NOMATCH);
188 			}
189 			string += sclen;
190 			break;
191 		case '\\':
192 			if (!(flags & FNM_NOESCAPE)) {
193 				pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
194 				    &patmbs);
195 				if (pclen == (size_t)-1 || pclen == (size_t)-2)
196 					return (FNM_NOMATCH);
197 				if (pclen == 0)
198 					pc = '\\';
199 				pattern += pclen;
200 			}
201 			/* FALLTHROUGH */
202 		default:
203 		norm:
204 			if (pc == sc)
205 				;
206 			else if ((flags & FNM_CASEFOLD) &&
207 				 (towlower(pc) == towlower(sc)))
208 				;
209 			else
210 				return (FNM_NOMATCH);
211 			string += sclen;
212 			break;
213 		}
214 	}
215 	/* NOTREACHED */
216 }
217 
218 static int
219 rangematch(pattern, test, flags, newp, patmbs)
220 	const char *pattern;
221 	wchar_t test;
222 	int flags;
223 	char **newp;
224 	mbstate_t *patmbs;
225 {
226 	int negate, ok;
227 	wchar_t c, c2;
228 	size_t pclen;
229 	const char *origpat;
230 	struct xlocale_collate *table =
231 		(struct xlocale_collate*)__get_locale()->components[XLC_COLLATE];
232 
233 	/*
234 	 * A bracket expression starting with an unquoted circumflex
235 	 * character produces unspecified results (IEEE 1003.2-1992,
236 	 * 3.13.2).  This implementation treats it like '!', for
237 	 * consistency with the regular expression syntax.
238 	 * J.T. Conklin (conklin@ngai.kaleida.com)
239 	 */
240 	if ( (negate = (*pattern == '!' || *pattern == '^')) )
241 		++pattern;
242 
243 	if (flags & FNM_CASEFOLD)
244 		test = towlower(test);
245 
246 	/*
247 	 * A right bracket shall lose its special meaning and represent
248 	 * itself in a bracket expression if it occurs first in the list.
249 	 * -- POSIX.2 2.8.3.2
250 	 */
251 	ok = 0;
252 	origpat = pattern;
253 	for (;;) {
254 		if (*pattern == ']' && pattern > origpat) {
255 			pattern++;
256 			break;
257 		} else if (*pattern == '\0') {
258 			return (RANGE_ERROR);
259 		} else if (*pattern == '/' && (flags & FNM_PATHNAME)) {
260 			return (RANGE_NOMATCH);
261 		} else if (*pattern == '\\' && !(flags & FNM_NOESCAPE))
262 			pattern++;
263 		pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
264 		if (pclen == (size_t)-1 || pclen == (size_t)-2)
265 			return (RANGE_NOMATCH);
266 		pattern += pclen;
267 
268 		if (flags & FNM_CASEFOLD)
269 			c = towlower(c);
270 
271 		if (*pattern == '-' && *(pattern + 1) != EOS &&
272 		    *(pattern + 1) != ']') {
273 			if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
274 				if (*pattern != EOS)
275 					pattern++;
276 			pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
277 			if (pclen == (size_t)-1 || pclen == (size_t)-2)
278 				return (RANGE_NOMATCH);
279 			pattern += pclen;
280 			if (c2 == EOS)
281 				return (RANGE_ERROR);
282 
283 			if (flags & FNM_CASEFOLD)
284 				c2 = towlower(c2);
285 
286 			if (table->__collate_load_error ?
287 			    c <= test && test <= c2 :
288 			       __collate_range_cmp(table, c, test) <= 0
289 			    && __collate_range_cmp(table, test, c2) <= 0
290 			   )
291 				ok = 1;
292 		} else if (c == test)
293 			ok = 1;
294 	}
295 
296 	*newp = (char *)pattern;
297 	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
298 }
299