1 /* $OpenBSD: fnmatch.c,v 1.22 2020/03/13 03:25:45 djm Exp $ */
3 /* Copyright (c) 2011, VMware, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the VMware, Inc. nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * Copyright (c) 2008, 2016 Todd C. Miller <millert@openbsd.org>
32 * Permission to use, copy, modify, and distribute this software for any
33 * purpose with or without fee is hereby granted, provided that the above
34 * copyright notice and this permission notice appear in all copies.
36 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
37 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
39 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
40 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
41 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
42 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45 /* Authored by William A. Rowe Jr. <wrowe; apache.org, vmware.com>, April 2011
47 * Derived from The Open Group Base Specifications Issue 7, IEEE Std 1003.1-2008
49 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/fnmatch.html
51 * Filename pattern matches defined in section 2.13, "Pattern Matching Notation"
52 * from chapter 2. "Shell Command Language"
53 * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13
54 * where; 1. A bracket expression starting with an unquoted <circumflex> '^'
55 * character CONTINUES to specify a non-matching list; 2. an explicit <period> '.'
56 * in a bracket expression matching list, e.g. "[.abc]" does NOT match a leading
57 * <period> in a filename; 3. a <left-square-bracket> '[' which does not introduce
58 * a valid bracket expression is treated as an ordinary character; 4. a differing
59 * number of consecutive slashes within pattern and string will NOT match;
60 * 5. a trailing '\' in FNM_ESCAPE mode is treated as an ordinary '\' character.
62 * Bracket expansion defined in section 9.3.5, "RE Bracket Expression",
63 * from chapter 9, "Regular Expressions"
64 * http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03_05
65 * with no support for collating symbols, equivalence class expressions or
66 * character class expressions. A partial range expression with a leading
67 * hyphen following a valid range expression will match only the ordinary
68 * <hyphen> and the ending character (e.g. "[a-m-z]" will match characters
69 * 'a' through 'm', a <hyphen> '-', or a 'z').
71 * Supports BSD extensions FNM_LEADING_DIR to match pattern to the end of one
72 * path segment of string, and FNM_CASEFOLD to ignore alpha case.
74 * NOTE: Only POSIX/C single byte locales are correctly supported at this time.
75 * Notably, non-POSIX locales with FNM_CASEFOLD produce undefined results,
76 * particularly in ranges of mixed case (e.g. "[A-z]") or spanning alpha and
77 * nonalpha characters within a range.
79 * XXX comments below indicate porting required for multi-byte character sets
80 * and non-POSIX locale collation orders; requires mbr* APIs to track shift
81 * state of pattern and string (rewinding pattern and string repeatedly).
83 * Certain parts of the code assume 0x00-0x3F are unique with any MBCS (e.g.
84 * UTF-8, SHIFT-JIS, etc). Any implementation allowing '\' as an alternate
85 * path delimiter must be aware that 0x5C is NOT unique within SHIFT-JIS.
88 /* OPENBSD ORIGINAL: lib/libc/gen/fnmatch.c */
97 #include "charclass.h"
100 #define RANGE_NOMATCH 0
101 #define RANGE_ERROR (-1)
104 classmatch(const char *pattern, char test, int foldcase, const char **ep)
106 const char * const mismatch = pattern;
109 int rval = RANGE_NOMATCH;
112 if (pattern[0] != '[' || pattern[1] != ':') {
118 if ((colon = strchr(pattern, ':')) == NULL || colon[1] != ']') {
123 len = (size_t)(colon - pattern);
125 if (foldcase && strncmp(pattern, "upper:]", 7) == 0)
127 for (cc = cclasses; cc->name != NULL; cc++) {
128 if (!strncmp(pattern, cc->name, len) && cc->name[len] == '\0') {
129 if (cc->isctype((unsigned char)test))
134 if (cc->name == NULL) {
135 /* invalid character class, treat as normal text */
142 /* Most MBCS/collation/case issues handled here. Wildcard '*' is not handled.
143 * EOS '\0' and the FNM_PATHNAME '/' delimiters are not advanced over,
144 * however the "\/" sequence is advanced to '/'.
146 * Both pattern and string are **char to support pointer increment of arbitrary
147 * multibyte characters for the given locale, in a later iteration of this code
149 static int fnmatch_ch(const char **pattern, const char **string, int flags)
151 const char * const mismatch = *pattern;
152 const int nocase = !!(flags & FNM_CASEFOLD);
153 const int escape = !(flags & FNM_NOESCAPE);
154 const int slash = !!(flags & FNM_PATHNAME);
155 int result = FNM_NOMATCH;
159 if (**pattern == '[') {
162 /* Handle negation, either leading ! or ^ operators */
163 negate = (**pattern == '!') || (**pattern == '^');
167 /* ']' is an ordinary char at the start of the range pattern */
168 if (**pattern == ']')
169 goto leadingclosebrace;
172 if (**pattern == ']') {
174 /* XXX: Fix for MBCS character width */
176 return (result ^ negate);
179 if (escape && (**pattern == '\\')) {
182 /* Patterns must terminate with ']', not EOS */
187 /* Patterns must terminate with ']' not '/' */
188 if (slash && (**pattern == '/'))
191 /* Match character classes. */
192 switch (classmatch(*pattern, **string, nocase, pattern)) {
197 /* Valid character class but no match. */
200 /* Not a valid character class. */
207 /* Look at only well-formed range patterns;
208 * "x-]" is not allowed unless escaped ("x-\]")
209 * XXX: Fix for locale/MBCS character width
211 if (((*pattern)[1] == '-') && ((*pattern)[2] != ']')) {
213 *pattern += (escape && ((*pattern)[2] == '\\')) ? 3 : 2;
216 * NOT a properly balanced [expr] pattern, EOS
217 * terminated or ranges containing a slash in
218 * FNM_PATHNAME mode pattern fall out to to the
219 * rewind and test '[' literal code path.
221 if (!**pattern || (slash && (**pattern == '/')))
224 /* XXX: handle locale/MBCS comparison, advance by MBCS char width */
225 if ((**string >= *startch) && (**string <= **pattern))
228 (isupper((unsigned char)**string) ||
229 isupper((unsigned char)*startch) ||
230 isupper((unsigned char)**pattern)) &&
231 (tolower((unsigned char)**string) >=
232 tolower((unsigned char)*startch)) &&
233 (tolower((unsigned char)**string) <=
234 tolower((unsigned char)**pattern)))
241 /* XXX: handle locale/MBCS comparison, advance by MBCS char width */
242 if ((**string == **pattern))
244 else if (nocase && (isupper((unsigned char)**string) ||
245 isupper((unsigned char)**pattern)) &&
246 (tolower((unsigned char)**string) ==
247 tolower((unsigned char)**pattern)))
253 * NOT a properly balanced [expr] pattern;
254 * Rewind and reset result to test '[' literal
257 result = FNM_NOMATCH;
258 } else if (**pattern == '?') {
259 /* Optimize '?' match before unescaping **pattern */
260 if (!**string || (slash && (**string == '/')))
263 goto fnmatch_ch_success;
264 } else if (escape && (**pattern == '\\') && (*pattern)[1]) {
268 /* XXX: handle locale/MBCS comparison, advance by the MBCS char width */
269 if (**string == **pattern)
271 else if (nocase && (isupper((unsigned char)**string) ||
272 isupper((unsigned char)**pattern)) &&
273 (tolower((unsigned char)**string) ==
274 tolower((unsigned char)**pattern)))
277 /* Refuse to advance over trailing slash or NULs */
278 if (**string == '\0' || **pattern == '\0' ||
279 (slash && ((**string == '/') || (**pattern == '/'))))
289 int fnmatch(const char *pattern, const char *string, int flags)
291 static const char dummystring[2] = {' ', 0};
292 const int escape = !(flags & FNM_NOESCAPE);
293 const int slash = !!(flags & FNM_PATHNAME);
294 const int leading_dir = !!(flags & FNM_LEADING_DIR);
295 const char *dummyptr, *matchptr, *strendseg;
297 /* For '*' wild processing only; suppress 'used before initialization'
298 * warnings with dummy initialization values;
300 const char *strstartseg = NULL;
301 const char *mismatch = NULL;
307 while (*pattern && *string) {
309 * Pre-decode "\/" which has no special significance, and
310 * match balanced slashes, starting a new segment pattern.
312 if (slash && escape && (*pattern == '\\') && (pattern[1] == '/'))
314 if (slash && (*pattern == '/') && (*string == '/')) {
321 * At the beginning of each segment, validate leading period
324 if ((flags & FNM_PERIOD) && (*string == '.')) {
327 else if (escape && (*pattern == '\\') && (pattern[1] == '.'))
335 * Determine the end of string segment. Presumes '/'
336 * character is unique, not composite in any MBCS encoding
339 strendseg = strchr(string, '/');
341 strendseg = strchr(string, '\0');
343 strendseg = strchr(string, '\0');
347 * Allow pattern '*' to be consumed even with no remaining
351 if ((string > strendseg) ||
352 ((string == strendseg) && (*pattern != '*')))
355 if (slash && ((*pattern == '/') ||
356 (escape && (*pattern == '\\') && (pattern[1] == '/'))))
360 * Reduce groups of '*' and '?' to n '?' matches
361 * followed by one '*' test for simplicity.
363 for (wild = 0; (*pattern == '*') || (*pattern == '?'); ++pattern) {
364 if (*pattern == '*') {
366 } else if (string < strendseg) { /* && (*pattern == '?') */
367 /* XXX: Advance 1 char for MBCS locale */
370 else { /* (string >= strendseg) && (*pattern == '?') */
376 strstartseg = string;
380 * Count fixed (non '*') char matches remaining
381 * in pattern * excluding '/' (or "\/") and '*'.
383 for (matchptr = pattern, matchlen = 0; 1; ++matchlen) {
384 if ((*matchptr == '\0') ||
385 (slash && ((*matchptr == '/') ||
386 (escape && (*matchptr == '\\') &&
387 (matchptr[1] == '/'))))) {
388 /* Compare precisely this many
389 * trailing string chars, the
390 * resulting match needs no
393 /* XXX: Adjust for MBCS */
394 if (string + matchlen > strendseg)
397 string = strendseg - matchlen;
402 if (*matchptr == '*') {
404 * Ensure at least this many
405 * trailing string chars remain
406 * for the first comparison.
408 /* XXX: Adjust for MBCS */
409 if (string + matchlen > strendseg)
413 * Begin first wild comparison
414 * at the current position.
420 * Skip forward in pattern by a single
421 * character match Use a dummy
422 * fnmatch_ch() test to count one
425 /* XXX: Adjust for MBCS */
426 if (escape && (*matchptr == '\\') &&
429 } else if (*matchptr == '[') {
430 dummyptr = dummystring;
431 fnmatch_ch(&matchptr, &dummyptr,
439 /* Incrementally match string against the pattern. */
440 while (*pattern && (string < strendseg)) {
441 /* Success; begin a new wild pattern search. */
445 if (slash && ((*string == '/') ||
446 (*pattern == '/') || (escape &&
447 (*pattern == '\\') && (pattern[1] == '/'))))
451 * Compare ch's (the pattern is advanced over
452 * "\/" to the '/', but slashes will mismatch,
453 * and are not consumed).
455 if (!fnmatch_ch(&pattern, &string, flags))
459 * Failed to match, loop against next char
460 * offset of string segment until not enough
461 * string chars remain to match the fixed
465 /* XXX: Advance 1 char for MBCS locale */
466 string = ++strstartseg;
467 if (string + matchlen > strendseg)
477 if (*string && !((slash || leading_dir) && (*string == '/')))
480 if (*pattern && !(slash && ((*pattern == '/') ||
481 (escape && (*pattern == '\\') && (pattern[1] == '/')))))
484 if (leading_dir && !*pattern && *string == '/')
488 /* Where both pattern and string are at EOS, declare success. */
489 if (!*string && !*pattern)
492 /* Pattern didn't match to the end of string. */
495 #endif /* HAVE_FNMATCH */