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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright 2013 Garrett D'Amore <garrett@damore.org> 35 * Copyright 2017 Nexenta Systems, Inc. 36 */ 37 38 /* 39 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. 40 * Compares a filename or pathname to a pattern. 41 */ 42 43 /* 44 * Some notes on multibyte character support: 45 * 1. Patterns with illegal byte sequences match nothing. 46 * 2. Illegal byte sequences in the "string" argument are handled by treating 47 * them as single-byte characters with a value of the first byte of the 48 * sequence cast to wchar_t. 49 * 3. Multibyte conversion state objects (mbstate_t) are passed around and 50 * used for most, but not all, conversions. Further work will be required 51 * to support state-dependent encodings. 52 */ 53 54 #include "lint.h" 55 #include <fnmatch.h> 56 #include <limits.h> 57 #include <string.h> 58 #include <wchar.h> 59 #include <xlocale.h> 60 #include <wctype.h> 61 #include "localeimpl.h" 62 #include "collate.h" 63 64 #define EOS '\0' 65 66 #define RANGE_MATCH 1 67 #define RANGE_NOMATCH 0 68 #define RANGE_ERROR (-1) 69 70 static int rangematch(const char *, wchar_t, int, char **, mbstate_t *, 71 locale_t); 72 static int fnmatch1(const char *, const char *, const char *, int, mbstate_t, 73 mbstate_t, locale_t); 74 75 int 76 fnmatch(const char *pattern, const char *string, int flags) 77 { 78 locale_t loc = uselocale(NULL); 79 static const mbstate_t initial = { 0 }; 80 81 return (fnmatch1(pattern, string, string, flags, initial, initial, 82 loc)); 83 } 84 85 static int 86 fnmatch1(const char *pattern, const char *string, const char *stringstart, 87 int flags, mbstate_t patmbs, mbstate_t strmbs, locale_t loc) 88 { 89 const char *bt_pattern, *bt_string; 90 mbstate_t bt_patmbs, bt_strmbs; 91 char *newp; 92 char c; 93 wchar_t pc, sc; 94 size_t pclen, sclen; 95 96 bt_pattern = bt_string = NULL; 97 for (;;) { 98 pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX, &patmbs, loc); 99 if (pclen == (size_t)-1 || pclen == (size_t)-2) 100 return (FNM_NOMATCH); 101 pattern += pclen; 102 sclen = mbrtowc_l(&sc, string, MB_LEN_MAX, &strmbs, loc); 103 if (sclen == (size_t)-1 || sclen == (size_t)-2) { 104 sc = (unsigned char)*string; 105 sclen = 1; 106 (void) memset(&strmbs, 0, sizeof (strmbs)); 107 } 108 switch (pc) { 109 case EOS: 110 /* 111 * Removed FNM_LEADING_DIR, as it is not present 112 * on Solaris. 113 */ 114 if (sc == EOS) 115 return (0); 116 goto backtrack; 117 case '?': 118 if (sc == EOS) 119 return (FNM_NOMATCH); 120 if (sc == '/' && (flags & FNM_PATHNAME)) 121 goto backtrack; 122 if (sc == '.' && (flags & FNM_PERIOD) && 123 (string == stringstart || 124 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 125 goto backtrack; 126 string += sclen; 127 break; 128 case '*': 129 c = *pattern; 130 /* Collapse multiple stars. */ 131 while (c == '*') 132 c = *++pattern; 133 134 if (sc == '.' && (flags & FNM_PERIOD) && 135 (string == stringstart || 136 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 137 goto backtrack; 138 139 /* Optimize for pattern with * at end or before /. */ 140 if (c == EOS) 141 if (flags & FNM_PATHNAME) 142 return (strchr(string, '/') == NULL ? 143 0 : FNM_NOMATCH); 144 else 145 return (0); 146 else if (c == '/' && flags & FNM_PATHNAME) { 147 if ((string = strchr(string, '/')) == NULL) 148 return (FNM_NOMATCH); 149 break; 150 } 151 152 /* 153 * First try the shortest match for the '*' that 154 * could work. We can forget any earlier '*' since 155 * there is no way having it match more characters 156 * can help us, given that we are already here. 157 */ 158 bt_pattern = pattern, bt_patmbs = patmbs; 159 bt_string = string, bt_strmbs = strmbs; 160 break; 161 case '[': 162 if (sc == EOS) 163 return (FNM_NOMATCH); 164 if (sc == '/' && (flags & FNM_PATHNAME)) 165 goto backtrack; 166 if (sc == '.' && (flags & FNM_PERIOD) && 167 (string == stringstart || 168 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 169 goto backtrack; 170 171 switch (rangematch(pattern, sc, flags, &newp, 172 &patmbs, loc)) { 173 case RANGE_ERROR: 174 goto norm; 175 case RANGE_MATCH: 176 pattern = newp; 177 break; 178 case RANGE_NOMATCH: 179 goto backtrack; 180 } 181 string += sclen; 182 break; 183 case '\\': 184 if (!(flags & FNM_NOESCAPE)) { 185 pclen = mbrtowc_l(&pc, pattern, MB_LEN_MAX, 186 &patmbs, loc); 187 if (pclen == (size_t)-1 || pclen == (size_t)-2) 188 return (FNM_NOMATCH); 189 if (pclen == 0) 190 pc = '\\'; 191 pattern += pclen; 192 } 193 /* FALLTHROUGH */ 194 default: 195 norm: 196 string += sclen; 197 if (pc == sc) 198 break; 199 else if ((flags & FNM_IGNORECASE) && 200 (towlower_l(pc, loc) == towlower_l(sc, loc))) 201 break; 202 else { 203 backtrack: 204 /* 205 * If we have a mismatch (other than hitting 206 * the end of the string), go back to the last 207 * '*' seen and have it match one additional 208 * character. 209 */ 210 if (bt_pattern == NULL) 211 return (FNM_NOMATCH); 212 sclen = mbrtowc_l(&sc, bt_string, MB_LEN_MAX, 213 &bt_strmbs, loc); 214 if (sclen == (size_t)-1 || 215 sclen == (size_t)-2) { 216 sc = (unsigned char)*bt_string; 217 sclen = 1; 218 (void) memset(&bt_strmbs, 0, 219 sizeof (bt_strmbs)); 220 } 221 if (sc == EOS) 222 return (FNM_NOMATCH); 223 if (sc == '/' && flags & FNM_PATHNAME) 224 return (FNM_NOMATCH); 225 bt_string += sclen; 226 pattern = bt_pattern, patmbs = bt_patmbs; 227 string = bt_string, strmbs = bt_strmbs; 228 } 229 break; 230 } 231 } 232 /* NOTREACHED */ 233 } 234 235 static int 236 rangematch(const char *pattern, wchar_t test, int flags, char **newp, 237 mbstate_t *patmbs, locale_t loc) 238 { 239 int negate, ok; 240 wchar_t c, c2; 241 size_t pclen; 242 const char *origpat; 243 244 /* 245 * A bracket expression starting with an unquoted circumflex 246 * character produces unspecified results (IEEE 1003.2-1992, 247 * 3.13.2). This implementation treats it like '!', for 248 * consistency with the regular expression syntax. 249 * J.T. Conklin (conklin@ngai.kaleida.com) 250 */ 251 if ((negate = (*pattern == '!' || *pattern == '^')) != 0) 252 ++pattern; 253 254 if (flags & FNM_IGNORECASE) 255 test = towlower_l(test, loc); 256 257 /* 258 * A right bracket shall lose its special meaning and represent 259 * itself in a bracket expression if it occurs first in the list. 260 * -- POSIX.2 2.8.3.2 261 */ 262 ok = 0; 263 origpat = pattern; 264 for (;;) { 265 if (*pattern == ']' && pattern > origpat) { 266 pattern++; 267 break; 268 } else if (*pattern == '\0') { 269 return (RANGE_ERROR); 270 } else if (*pattern == '/' && (flags & FNM_PATHNAME)) { 271 return (RANGE_NOMATCH); 272 } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE)) 273 pattern++; 274 pclen = mbrtowc_l(&c, pattern, MB_LEN_MAX, patmbs, loc); 275 if (pclen == (size_t)-1 || pclen == (size_t)-2) 276 return (RANGE_NOMATCH); 277 pattern += pclen; 278 279 if (flags & FNM_IGNORECASE) 280 c = towlower_l(c, loc); 281 282 if (*pattern == '-' && *(pattern + 1) != EOS && 283 *(pattern + 1) != ']') { 284 if (*++pattern == '\\' && !(flags & FNM_NOESCAPE)) 285 if (*pattern != EOS) 286 pattern++; 287 pclen = mbrtowc_l(&c2, pattern, MB_LEN_MAX, patmbs, 288 loc); 289 if (pclen == (size_t)-1 || pclen == (size_t)-2) 290 return (RANGE_NOMATCH); 291 pattern += pclen; 292 if (c2 == EOS) 293 return (RANGE_ERROR); 294 295 if (flags & FNM_IGNORECASE) 296 c2 = towlower_l(c2, loc); 297 298 if (loc->collate->lc_is_posix ? 299 c <= test && test <= c2 : 300 _collate_range_cmp(c, test, loc) <= 0 && 301 _collate_range_cmp(test, c2, loc) <= 0) 302 ok = 1; 303 } else if (c == test) 304 ok = 1; 305 } 306 307 *newp = (char *)pattern; 308 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); 309 } 310