fnmatch.c revision 304275
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: stable/11/lib/libc/gen/fnmatch.c 304275 2016-08-17 08:51:47Z ache $"); 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 79288029Srodrigcfnmatch(const char *pattern, const char *string, int flags) 801573Srgrimes{ 81132812Stjr static const mbstate_t initial; 82132812Stjr 83206711Sjilles return (fnmatch1(pattern, string, string, flags, initial, initial)); 84132812Stjr} 85132812Stjr 86132812Stjrstatic int 87288029Srodrigcfnmatch1(const char *pattern, const char *string, const char *stringstart, 88288029Srodrigc int flags, mbstate_t patmbs, mbstate_t strmbs) 89132812Stjr{ 90288309Sjilles const char *bt_pattern, *bt_string; 91288309Sjilles mbstate_t bt_patmbs, bt_strmbs; 9226484Sache char *newp; 93132812Stjr char c; 94132812Stjr wchar_t pc, sc; 95132812Stjr size_t pclen, sclen; 961573Srgrimes 97288309Sjilles bt_pattern = bt_string = NULL; 98206711Sjilles for (;;) { 99132812Stjr pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs); 100132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 101132812Stjr return (FNM_NOMATCH); 102132812Stjr pattern += pclen; 103132812Stjr sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs); 104132812Stjr if (sclen == (size_t)-1 || sclen == (size_t)-2) { 105132812Stjr sc = (unsigned char)*string; 106132812Stjr sclen = 1; 107132812Stjr memset(&strmbs, 0, sizeof(strmbs)); 108132812Stjr } 109132812Stjr switch (pc) { 1101573Srgrimes case EOS: 111132812Stjr if ((flags & FNM_LEADING_DIR) && sc == '/') 11219132Sache return (0); 113288309Sjilles if (sc == EOS) 114288309Sjilles return (0); 115288309Sjilles goto backtrack; 1161573Srgrimes case '?': 117132812Stjr if (sc == EOS) 1181573Srgrimes return (FNM_NOMATCH); 119132812Stjr if (sc == '/' && (flags & FNM_PATHNAME)) 120288309Sjilles goto backtrack; 121132812Stjr if (sc == '.' && (flags & FNM_PERIOD) && 1221573Srgrimes (string == stringstart || 1231573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 124288309Sjilles goto backtrack; 125132812Stjr string += sclen; 1261573Srgrimes break; 1271573Srgrimes case '*': 1281573Srgrimes c = *pattern; 1291573Srgrimes /* Collapse multiple stars. */ 1301573Srgrimes while (c == '*') 1311573Srgrimes c = *++pattern; 1321573Srgrimes 133132812Stjr if (sc == '.' && (flags & FNM_PERIOD) && 1341573Srgrimes (string == stringstart || 1351573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 136288309Sjilles goto backtrack; 1371573Srgrimes 1381573Srgrimes /* Optimize for pattern with * at end or before /. */ 1391573Srgrimes if (c == EOS) 1401573Srgrimes if (flags & FNM_PATHNAME) 14125269Sjdp return ((flags & FNM_LEADING_DIR) || 14225269Sjdp strchr(string, '/') == NULL ? 1431573Srgrimes 0 : FNM_NOMATCH); 1441573Srgrimes else 1451573Srgrimes return (0); 1461573Srgrimes else if (c == '/' && flags & FNM_PATHNAME) { 1471573Srgrimes if ((string = strchr(string, '/')) == NULL) 1481573Srgrimes return (FNM_NOMATCH); 1491573Srgrimes break; 1501573Srgrimes } 1511573Srgrimes 152288309Sjilles /* 153288309Sjilles * First try the shortest match for the '*' that 154288309Sjilles * could work. We can forget any earlier '*' since 155288309Sjilles * there is no way having it match more characters 156288309Sjilles * can help us, given that we are already here. 157288309Sjilles */ 158288309Sjilles bt_pattern = pattern, bt_patmbs = patmbs; 159288309Sjilles bt_string = string, bt_strmbs = strmbs; 160288309Sjilles break; 1611573Srgrimes case '[': 162132812Stjr if (sc == EOS) 1631573Srgrimes return (FNM_NOMATCH); 164132812Stjr if (sc == '/' && (flags & FNM_PATHNAME)) 165288309Sjilles goto backtrack; 166132812Stjr if (sc == '.' && (flags & FNM_PERIOD) && 16726486Sache (string == stringstart || 16826486Sache ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 169288309Sjilles goto backtrack; 17026486Sache 171132812Stjr switch (rangematch(pattern, sc, flags, &newp, 172132812Stjr &patmbs)) { 17326484Sache case RANGE_ERROR: 17426484Sache goto norm; 17526484Sache case RANGE_MATCH: 17626484Sache pattern = newp; 17726484Sache break; 17826484Sache case RANGE_NOMATCH: 179288309Sjilles goto backtrack; 18026484Sache } 181132812Stjr string += sclen; 1821573Srgrimes break; 1831573Srgrimes case '\\': 1841573Srgrimes if (!(flags & FNM_NOESCAPE)) { 185132812Stjr pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, 186132812Stjr &patmbs); 187132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 188132812Stjr return (FNM_NOMATCH); 189132812Stjr pattern += pclen; 1901573Srgrimes } 1911573Srgrimes /* FALLTHROUGH */ 1921573Srgrimes default: 19326484Sache norm: 194288309Sjilles string += sclen; 195132812Stjr if (pc == sc) 19619059Swosch ; 19719132Sache else if ((flags & FNM_CASEFOLD) && 198132812Stjr (towlower(pc) == towlower(sc))) 19919059Swosch ; 200288309Sjilles else { 201288309Sjilles backtrack: 202288309Sjilles /* 203288309Sjilles * If we have a mismatch (other than hitting 204288309Sjilles * the end of the string), go back to the last 205288309Sjilles * '*' seen and have it match one additional 206288309Sjilles * character. 207288309Sjilles */ 208288309Sjilles if (bt_pattern == NULL) 209288309Sjilles return (FNM_NOMATCH); 210288309Sjilles sclen = mbrtowc(&sc, bt_string, MB_LEN_MAX, 211288309Sjilles &bt_strmbs); 212288309Sjilles if (sclen == (size_t)-1 || 213288309Sjilles sclen == (size_t)-2) { 214288309Sjilles sc = (unsigned char)*bt_string; 215288309Sjilles sclen = 1; 216288309Sjilles memset(&bt_strmbs, 0, 217288309Sjilles sizeof(bt_strmbs)); 218288309Sjilles } 219288309Sjilles if (sc == EOS) 220288309Sjilles return (FNM_NOMATCH); 221288309Sjilles if (sc == '/' && flags & FNM_PATHNAME) 222288309Sjilles return (FNM_NOMATCH); 223288309Sjilles bt_string += sclen; 224288309Sjilles pattern = bt_pattern, patmbs = bt_patmbs; 225288309Sjilles string = bt_string, strmbs = bt_strmbs; 226288309Sjilles } 2271573Srgrimes break; 2281573Srgrimes } 229132812Stjr } 2301573Srgrimes /* NOTREACHED */ 2311573Srgrimes} 2321573Srgrimes 23326484Sachestatic int 234288029Srodrigcrangematch(const char *pattern, wchar_t test, int flags, char **newp, 235288029Srodrigc mbstate_t *patmbs) 2361573Srgrimes{ 23726492Sache int negate, ok; 238132812Stjr wchar_t c, c2; 239132812Stjr size_t pclen; 240132812Stjr const char *origpat; 241227753Stheraven struct xlocale_collate *table = 242227753Stheraven (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; 2431573Srgrimes 2441573Srgrimes /* 2451573Srgrimes * A bracket expression starting with an unquoted circumflex 2461573Srgrimes * character produces unspecified results (IEEE 1003.2-1992, 2471573Srgrimes * 3.13.2). This implementation treats it like '!', for 2481573Srgrimes * consistency with the regular expression syntax. 2491573Srgrimes * J.T. Conklin (conklin@ngai.kaleida.com) 2501573Srgrimes */ 25126486Sache if ( (negate = (*pattern == '!' || *pattern == '^')) ) 2521573Srgrimes ++pattern; 2538870Srgrimes 25419132Sache if (flags & FNM_CASEFOLD) 255132812Stjr test = towlower(test); 25619059Swosch 25726484Sache /* 25826484Sache * A right bracket shall lose its special meaning and represent 25926484Sache * itself in a bracket expression if it occurs first in the list. 26026484Sache * -- POSIX.2 2.8.3.2 26126484Sache */ 26226492Sache ok = 0; 263132812Stjr origpat = pattern; 264132812Stjr for (;;) { 265132812Stjr if (*pattern == ']' && pattern > origpat) { 266132812Stjr pattern++; 267132812Stjr break; 268132812Stjr } else if (*pattern == '\0') { 26926484Sache return (RANGE_ERROR); 270132812Stjr } else if (*pattern == '/' && (flags & FNM_PATHNAME)) { 27126486Sache return (RANGE_NOMATCH); 272132812Stjr } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE)) 273132812Stjr pattern++; 274132812Stjr pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs); 275132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 276132812Stjr return (RANGE_NOMATCH); 277132812Stjr pattern += pclen; 27826486Sache 27919132Sache if (flags & FNM_CASEFOLD) 280132812Stjr c = towlower(c); 28119059Swosch 282132812Stjr if (*pattern == '-' && *(pattern + 1) != EOS && 283132812Stjr *(pattern + 1) != ']') { 284132812Stjr if (*++pattern == '\\' && !(flags & FNM_NOESCAPE)) 285132812Stjr if (*pattern != EOS) 286132812Stjr pattern++; 287132812Stjr pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs); 288132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 289132812Stjr return (RANGE_NOMATCH); 290132812Stjr pattern += pclen; 2911573Srgrimes if (c2 == EOS) 29226484Sache return (RANGE_ERROR); 29319059Swosch 29419132Sache if (flags & FNM_CASEFOLD) 295132812Stjr c2 = towlower(c2); 29619059Swosch 297227753Stheraven if (table->__collate_load_error ? 29824632Sache c <= test && test <= c2 : 299304275Sache __wcollate_range_cmp(c, test) <= 0 300304275Sache && __wcollate_range_cmp(test, c2) <= 0 30117533Sache ) 3021573Srgrimes ok = 1; 3031573Srgrimes } else if (c == test) 3041573Srgrimes ok = 1; 305132812Stjr } 30626492Sache 30726484Sache *newp = (char *)pattern; 30826484Sache return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); 3091573Srgrimes} 310