xref: /freebsd/lib/libc/gen/fnmatch.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  */
36 
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$");
42 
43 /*
44  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
45  * Compares a filename or pathname to a pattern.
46  */
47 
48 /*
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.
57  */
58 
59 #include <fnmatch.h>
60 #include <limits.h>
61 #include <string.h>
62 #include <wchar.h>
63 #include <wctype.h>
64 
65 #include "collate.h"
66 
67 #define	EOS	'\0'
68 
69 #define RANGE_MATCH     1
70 #define RANGE_NOMATCH   0
71 #define RANGE_ERROR     (-1)
72 
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);
75 
76 int
77 fnmatch(pattern, string, flags)
78 	const char *pattern, *string;
79 	int flags;
80 {
81 	static const mbstate_t initial;
82 
83 	return (fnmatch1(pattern, string, flags, initial, initial));
84 }
85 
86 static int
87 fnmatch1(pattern, string, flags, patmbs, strmbs)
88 	const char *pattern, *string;
89 	int flags;
90 	mbstate_t patmbs, strmbs;
91 {
92 	const char *stringstart;
93 	char *newp;
94 	char c;
95 	wchar_t pc, sc;
96 	size_t pclen, sclen;
97 
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);
102 		pattern += pclen;
103 		sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs);
104 		if (sclen == (size_t)-1 || sclen == (size_t)-2) {
105 			sc = (unsigned char)*string;
106 			sclen = 1;
107 			memset(&strmbs, 0, sizeof(strmbs));
108 		}
109 		switch (pc) {
110 		case EOS:
111 			if ((flags & FNM_LEADING_DIR) && sc == '/')
112 				return (0);
113 			return (sc == EOS ? 0 : FNM_NOMATCH);
114 		case '?':
115 			if (sc == EOS)
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);
123 			string += sclen;
124 			break;
125 		case '*':
126 			c = *pattern;
127 			/* Collapse multiple stars. */
128 			while (c == '*')
129 				c = *++pattern;
130 
131 			if (sc == '.' && (flags & FNM_PERIOD) &&
132 			    (string == stringstart ||
133 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
134 				return (FNM_NOMATCH);
135 
136 			/* Optimize for pattern with * at end or before /. */
137 			if (c == EOS)
138 				if (flags & FNM_PATHNAME)
139 					return ((flags & FNM_LEADING_DIR) ||
140 					    strchr(string, '/') == NULL ?
141 					    0 : FNM_NOMATCH);
142 				else
143 					return (0);
144 			else if (c == '/' && flags & FNM_PATHNAME) {
145 				if ((string = strchr(string, '/')) == NULL)
146 					return (FNM_NOMATCH);
147 				break;
148 			}
149 
150 			/* General case, use recursion. */
151 			while (sc != EOS) {
152 				if (!fnmatch1(pattern, string,
153 				    flags & ~FNM_PERIOD, patmbs, strmbs))
154 					return (0);
155 				sclen = mbrtowc(&sc, string, MB_LEN_MAX,
156 				    &strmbs);
157 				if (sclen == (size_t)-1 ||
158 				    sclen == (size_t)-2) {
159 					sc = (unsigned char)*string;
160 					sclen = 1;
161 					memset(&strmbs, 0, sizeof(strmbs));
162 				}
163 				if (sc == '/' && flags & FNM_PATHNAME)
164 					break;
165 				string += sclen;
166 			}
167 			return (FNM_NOMATCH);
168 		case '[':
169 			if (sc == EOS)
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);
177 
178 			switch (rangematch(pattern, sc, flags, &newp,
179 			    &patmbs)) {
180 			case RANGE_ERROR:
181 				goto norm;
182 			case RANGE_MATCH:
183 				pattern = newp;
184 				break;
185 			case RANGE_NOMATCH:
186 				return (FNM_NOMATCH);
187 			}
188 			string += sclen;
189 			break;
190 		case '\\':
191 			if (!(flags & FNM_NOESCAPE)) {
192 				pclen = mbrtowc(&pc, pattern, MB_LEN_MAX,
193 				    &patmbs);
194 				if (pclen == (size_t)-1 || pclen == (size_t)-2)
195 					return (FNM_NOMATCH);
196 				if (pclen == 0)
197 					pc = '\\';
198 				pattern += pclen;
199 			}
200 			/* FALLTHROUGH */
201 		default:
202 		norm:
203 			if (pc == sc)
204 				;
205 			else if ((flags & FNM_CASEFOLD) &&
206 				 (towlower(pc) == towlower(sc)))
207 				;
208 			else
209 				return (FNM_NOMATCH);
210 			string += sclen;
211 			break;
212 		}
213 	}
214 	/* NOTREACHED */
215 }
216 
217 static int
218 rangematch(pattern, test, flags, newp, patmbs)
219 	const char *pattern;
220 	wchar_t test;
221 	int flags;
222 	char **newp;
223 	mbstate_t *patmbs;
224 {
225 	int negate, ok;
226 	wchar_t c, c2;
227 	size_t pclen;
228 	const char *origpat;
229 
230 	/*
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)
236 	 */
237 	if ( (negate = (*pattern == '!' || *pattern == '^')) )
238 		++pattern;
239 
240 	if (flags & FNM_CASEFOLD)
241 		test = towlower(test);
242 
243 	/*
244 	 * A right bracket shall lose its special meaning and represent
245 	 * itself in a bracket expression if it occurs first in the list.
246 	 * -- POSIX.2 2.8.3.2
247 	 */
248 	ok = 0;
249 	origpat = pattern;
250 	for (;;) {
251 		if (*pattern == ']' && pattern > origpat) {
252 			pattern++;
253 			break;
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))
259 			pattern++;
260 		pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs);
261 		if (pclen == (size_t)-1 || pclen == (size_t)-2)
262 			return (RANGE_NOMATCH);
263 		pattern += pclen;
264 
265 		if (flags & FNM_CASEFOLD)
266 			c = towlower(c);
267 
268 		if (*pattern == '-' && *(pattern + 1) != EOS &&
269 		    *(pattern + 1) != ']') {
270 			if (*++pattern == '\\' && !(flags & FNM_NOESCAPE))
271 				if (*pattern != EOS)
272 					pattern++;
273 			pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs);
274 			if (pclen == (size_t)-1 || pclen == (size_t)-2)
275 				return (RANGE_NOMATCH);
276 			pattern += pclen;
277 			if (c2 == EOS)
278 				return (RANGE_ERROR);
279 
280 			if (flags & FNM_CASEFOLD)
281 				c2 = towlower(c2);
282 
283 			if (__collate_load_error ?
284 			    c <= test && test <= c2 :
285 			       __collate_range_cmp(c, test) <= 0
286 			    && __collate_range_cmp(test, c2) <= 0
287 			   )
288 				ok = 1;
289 		} else if (c == test)
290 			ok = 1;
291 	}
292 
293 	*newp = (char *)pattern;
294 	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
295 }
296