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$"); 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 791573Srgrimesfnmatch(pattern, string, flags) 801573Srgrimes const char *pattern, *string; 811573Srgrimes int flags; 821573Srgrimes{ 83132812Stjr static const mbstate_t initial; 84132812Stjr 85206711Sjilles return (fnmatch1(pattern, string, string, flags, initial, initial)); 86132812Stjr} 87132812Stjr 88132812Stjrstatic int 89206711Sjillesfnmatch1(pattern, string, stringstart, flags, patmbs, strmbs) 90206711Sjilles const char *pattern, *string, *stringstart; 91132812Stjr int flags; 92132812Stjr mbstate_t patmbs, strmbs; 93132812Stjr{ 9426484Sache char *newp; 95132812Stjr char c; 96132812Stjr wchar_t pc, sc; 97132812Stjr size_t pclen, sclen; 981573Srgrimes 99206711Sjilles for (;;) { 100132812Stjr pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, &patmbs); 101132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 102132812Stjr return (FNM_NOMATCH); 103132812Stjr pattern += pclen; 104132812Stjr sclen = mbrtowc(&sc, string, MB_LEN_MAX, &strmbs); 105132812Stjr if (sclen == (size_t)-1 || sclen == (size_t)-2) { 106132812Stjr sc = (unsigned char)*string; 107132812Stjr sclen = 1; 108132812Stjr memset(&strmbs, 0, sizeof(strmbs)); 109132812Stjr } 110132812Stjr switch (pc) { 1111573Srgrimes case EOS: 112132812Stjr if ((flags & FNM_LEADING_DIR) && sc == '/') 11319132Sache return (0); 114132812Stjr return (sc == EOS ? 0 : FNM_NOMATCH); 1151573Srgrimes case '?': 116132812Stjr if (sc == EOS) 1171573Srgrimes return (FNM_NOMATCH); 118132812Stjr if (sc == '/' && (flags & FNM_PATHNAME)) 1191573Srgrimes return (FNM_NOMATCH); 120132812Stjr if (sc == '.' && (flags & FNM_PERIOD) && 1211573Srgrimes (string == stringstart || 1221573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 1231573Srgrimes return (FNM_NOMATCH); 124132812Stjr string += sclen; 1251573Srgrimes break; 1261573Srgrimes case '*': 1271573Srgrimes c = *pattern; 1281573Srgrimes /* Collapse multiple stars. */ 1291573Srgrimes while (c == '*') 1301573Srgrimes c = *++pattern; 1311573Srgrimes 132132812Stjr if (sc == '.' && (flags & FNM_PERIOD) && 1331573Srgrimes (string == stringstart || 1341573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 1351573Srgrimes return (FNM_NOMATCH); 1361573Srgrimes 1371573Srgrimes /* Optimize for pattern with * at end or before /. */ 1381573Srgrimes if (c == EOS) 1391573Srgrimes if (flags & FNM_PATHNAME) 14025269Sjdp return ((flags & FNM_LEADING_DIR) || 14125269Sjdp strchr(string, '/') == NULL ? 1421573Srgrimes 0 : FNM_NOMATCH); 1431573Srgrimes else 1441573Srgrimes return (0); 1451573Srgrimes else if (c == '/' && flags & FNM_PATHNAME) { 1461573Srgrimes if ((string = strchr(string, '/')) == NULL) 1471573Srgrimes return (FNM_NOMATCH); 1481573Srgrimes break; 1491573Srgrimes } 1501573Srgrimes 1511573Srgrimes /* General case, use recursion. */ 152132812Stjr while (sc != EOS) { 153206711Sjilles if (!fnmatch1(pattern, string, stringstart, 154206711Sjilles flags, patmbs, strmbs)) 1551573Srgrimes return (0); 156132812Stjr sclen = mbrtowc(&sc, string, MB_LEN_MAX, 157132812Stjr &strmbs); 158132812Stjr if (sclen == (size_t)-1 || 159132812Stjr sclen == (size_t)-2) { 160132812Stjr sc = (unsigned char)*string; 161132812Stjr sclen = 1; 162132812Stjr memset(&strmbs, 0, sizeof(strmbs)); 163132812Stjr } 164132812Stjr if (sc == '/' && flags & FNM_PATHNAME) 1651573Srgrimes break; 166132812Stjr string += sclen; 1671573Srgrimes } 1681573Srgrimes return (FNM_NOMATCH); 1691573Srgrimes case '[': 170132812Stjr if (sc == EOS) 1711573Srgrimes return (FNM_NOMATCH); 172132812Stjr if (sc == '/' && (flags & FNM_PATHNAME)) 1731573Srgrimes return (FNM_NOMATCH); 174132812Stjr if (sc == '.' && (flags & FNM_PERIOD) && 17526486Sache (string == stringstart || 17626486Sache ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 17726486Sache return (FNM_NOMATCH); 17826486Sache 179132812Stjr switch (rangematch(pattern, sc, flags, &newp, 180132812Stjr &patmbs)) { 18126484Sache case RANGE_ERROR: 18226484Sache goto norm; 18326484Sache case RANGE_MATCH: 18426484Sache pattern = newp; 18526484Sache break; 18626484Sache case RANGE_NOMATCH: 1871573Srgrimes return (FNM_NOMATCH); 18826484Sache } 189132812Stjr string += sclen; 1901573Srgrimes break; 1911573Srgrimes case '\\': 1921573Srgrimes if (!(flags & FNM_NOESCAPE)) { 193132812Stjr pclen = mbrtowc(&pc, pattern, MB_LEN_MAX, 194132812Stjr &patmbs); 195132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 196132812Stjr return (FNM_NOMATCH); 197132812Stjr pattern += pclen; 1981573Srgrimes } 1991573Srgrimes /* FALLTHROUGH */ 2001573Srgrimes default: 20126484Sache norm: 202132812Stjr if (pc == sc) 20319059Swosch ; 20419132Sache else if ((flags & FNM_CASEFOLD) && 205132812Stjr (towlower(pc) == towlower(sc))) 20619059Swosch ; 20719059Swosch else 2081573Srgrimes return (FNM_NOMATCH); 209132812Stjr string += sclen; 2101573Srgrimes break; 2111573Srgrimes } 212132812Stjr } 2131573Srgrimes /* NOTREACHED */ 2141573Srgrimes} 2151573Srgrimes 21626484Sachestatic int 217132812Stjrrangematch(pattern, test, flags, newp, patmbs) 2181573Srgrimes const char *pattern; 219132812Stjr wchar_t test; 22019132Sache int flags; 22126484Sache char **newp; 222132812Stjr mbstate_t *patmbs; 2231573Srgrimes{ 22426492Sache int negate, ok; 225132812Stjr wchar_t c, c2; 226132812Stjr size_t pclen; 227132812Stjr const char *origpat; 228227753Stheraven struct xlocale_collate *table = 229227753Stheraven (struct xlocale_collate*)__get_locale()->components[XLC_COLLATE]; 2301573Srgrimes 2311573Srgrimes /* 2321573Srgrimes * A bracket expression starting with an unquoted circumflex 2331573Srgrimes * character produces unspecified results (IEEE 1003.2-1992, 2341573Srgrimes * 3.13.2). This implementation treats it like '!', for 2351573Srgrimes * consistency with the regular expression syntax. 2361573Srgrimes * J.T. Conklin (conklin@ngai.kaleida.com) 2371573Srgrimes */ 23826486Sache if ( (negate = (*pattern == '!' || *pattern == '^')) ) 2391573Srgrimes ++pattern; 2408870Srgrimes 24119132Sache if (flags & FNM_CASEFOLD) 242132812Stjr test = towlower(test); 24319059Swosch 24426484Sache /* 24526484Sache * A right bracket shall lose its special meaning and represent 24626484Sache * itself in a bracket expression if it occurs first in the list. 24726484Sache * -- POSIX.2 2.8.3.2 24826484Sache */ 24926492Sache ok = 0; 250132812Stjr origpat = pattern; 251132812Stjr for (;;) { 252132812Stjr if (*pattern == ']' && pattern > origpat) { 253132812Stjr pattern++; 254132812Stjr break; 255132812Stjr } else if (*pattern == '\0') { 25626484Sache return (RANGE_ERROR); 257132812Stjr } else if (*pattern == '/' && (flags & FNM_PATHNAME)) { 25826486Sache return (RANGE_NOMATCH); 259132812Stjr } else if (*pattern == '\\' && !(flags & FNM_NOESCAPE)) 260132812Stjr pattern++; 261132812Stjr pclen = mbrtowc(&c, pattern, MB_LEN_MAX, patmbs); 262132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 263132812Stjr return (RANGE_NOMATCH); 264132812Stjr pattern += pclen; 26526486Sache 26619132Sache if (flags & FNM_CASEFOLD) 267132812Stjr c = towlower(c); 26819059Swosch 269132812Stjr if (*pattern == '-' && *(pattern + 1) != EOS && 270132812Stjr *(pattern + 1) != ']') { 271132812Stjr if (*++pattern == '\\' && !(flags & FNM_NOESCAPE)) 272132812Stjr if (*pattern != EOS) 273132812Stjr pattern++; 274132812Stjr pclen = mbrtowc(&c2, pattern, MB_LEN_MAX, patmbs); 275132812Stjr if (pclen == (size_t)-1 || pclen == (size_t)-2) 276132812Stjr return (RANGE_NOMATCH); 277132812Stjr pattern += pclen; 2781573Srgrimes if (c2 == EOS) 27926484Sache return (RANGE_ERROR); 28019059Swosch 28119132Sache if (flags & FNM_CASEFOLD) 282132812Stjr c2 = towlower(c2); 28319059Swosch 284227753Stheraven if (table->__collate_load_error ? 28524632Sache c <= test && test <= c2 : 286227753Stheraven __collate_range_cmp(table, c, test) <= 0 287227753Stheraven && __collate_range_cmp(table, test, c2) <= 0 28817533Sache ) 2891573Srgrimes ok = 1; 2901573Srgrimes } else if (c == test) 2911573Srgrimes ok = 1; 292132812Stjr } 29326492Sache 29426484Sache *newp = (char *)pattern; 29526484Sache return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); 2961573Srgrimes} 297