fnmatch.c revision 25269
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 * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 3. All advertising materials mentioning features or use of this software 171573Srgrimes * must display the following acknowledgement: 181573Srgrimes * This product includes software developed by the University of 191573Srgrimes * California, Berkeley and its contributors. 201573Srgrimes * 4. Neither the name of the University nor the names of its contributors 211573Srgrimes * may be used to endorse or promote products derived from this software 221573Srgrimes * without specific prior written permission. 231573Srgrimes * 241573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341573Srgrimes * SUCH DAMAGE. 351573Srgrimes */ 361573Srgrimes 371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 381573Srgrimesstatic char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; 391573Srgrimes#endif /* LIBC_SCCS and not lint */ 401573Srgrimes 411573Srgrimes/* 421573Srgrimes * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. 431573Srgrimes * Compares a filename or pathname to a pattern. 441573Srgrimes */ 451573Srgrimes 4619059Swosch#include <ctype.h> 471573Srgrimes#include <fnmatch.h> 481573Srgrimes#include <string.h> 4919059Swosch#include <stdio.h> 501573Srgrimes 5119276Sache#include "collate.h" 5219276Sache 531573Srgrimes#define EOS '\0' 541573Srgrimes 5519132Sachestatic const char *rangematch __P((const char *, char, int)); 561573Srgrimes 571573Srgrimesint 581573Srgrimesfnmatch(pattern, string, flags) 591573Srgrimes const char *pattern, *string; 601573Srgrimes int flags; 611573Srgrimes{ 621573Srgrimes const char *stringstart; 631573Srgrimes char c, test; 641573Srgrimes 651573Srgrimes for (stringstart = string;;) 661573Srgrimes switch (c = *pattern++) { 671573Srgrimes case EOS: 6819132Sache if ((flags & FNM_LEADING_DIR) && *string == '/') 6919132Sache return (0); 701573Srgrimes return (*string == EOS ? 0 : FNM_NOMATCH); 711573Srgrimes case '?': 721573Srgrimes if (*string == EOS) 731573Srgrimes return (FNM_NOMATCH); 741573Srgrimes if (*string == '/' && (flags & FNM_PATHNAME)) 751573Srgrimes return (FNM_NOMATCH); 761573Srgrimes if (*string == '.' && (flags & FNM_PERIOD) && 771573Srgrimes (string == stringstart || 781573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 791573Srgrimes return (FNM_NOMATCH); 801573Srgrimes ++string; 811573Srgrimes break; 821573Srgrimes case '*': 831573Srgrimes c = *pattern; 841573Srgrimes /* Collapse multiple stars. */ 851573Srgrimes while (c == '*') 861573Srgrimes c = *++pattern; 871573Srgrimes 881573Srgrimes if (*string == '.' && (flags & FNM_PERIOD) && 891573Srgrimes (string == stringstart || 901573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 911573Srgrimes return (FNM_NOMATCH); 921573Srgrimes 931573Srgrimes /* Optimize for pattern with * at end or before /. */ 941573Srgrimes if (c == EOS) 951573Srgrimes if (flags & FNM_PATHNAME) 9625269Sjdp return ((flags & FNM_LEADING_DIR) || 9725269Sjdp strchr(string, '/') == NULL ? 981573Srgrimes 0 : FNM_NOMATCH); 991573Srgrimes else 1001573Srgrimes return (0); 1011573Srgrimes else if (c == '/' && flags & FNM_PATHNAME) { 1021573Srgrimes if ((string = strchr(string, '/')) == NULL) 1031573Srgrimes return (FNM_NOMATCH); 1041573Srgrimes break; 1051573Srgrimes } 1061573Srgrimes 1071573Srgrimes /* General case, use recursion. */ 1081573Srgrimes while ((test = *string) != EOS) { 1091573Srgrimes if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 1101573Srgrimes return (0); 1111573Srgrimes if (test == '/' && flags & FNM_PATHNAME) 1121573Srgrimes break; 1131573Srgrimes ++string; 1141573Srgrimes } 1151573Srgrimes return (FNM_NOMATCH); 1161573Srgrimes case '[': 1171573Srgrimes if (*string == EOS) 1181573Srgrimes return (FNM_NOMATCH); 1191573Srgrimes if (*string == '/' && flags & FNM_PATHNAME) 1201573Srgrimes return (FNM_NOMATCH); 1211573Srgrimes if ((pattern = 1221573Srgrimes rangematch(pattern, *string, flags)) == NULL) 1231573Srgrimes return (FNM_NOMATCH); 1241573Srgrimes ++string; 1251573Srgrimes break; 1261573Srgrimes case '\\': 1271573Srgrimes if (!(flags & FNM_NOESCAPE)) { 1281573Srgrimes if ((c = *pattern++) == EOS) { 1291573Srgrimes c = '\\'; 1301573Srgrimes --pattern; 1311573Srgrimes } 1321573Srgrimes } 1331573Srgrimes /* FALLTHROUGH */ 1341573Srgrimes default: 13519059Swosch if (c == *string) 13619059Swosch ; 13719132Sache else if ((flags & FNM_CASEFOLD) && 13819132Sache (tolower((unsigned char)c) == 13919132Sache tolower((unsigned char)*string))) 14019059Swosch ; 14119059Swosch else 1421573Srgrimes return (FNM_NOMATCH); 14319059Swosch string++; 1441573Srgrimes break; 1451573Srgrimes } 1461573Srgrimes /* NOTREACHED */ 1471573Srgrimes} 1481573Srgrimes 1491573Srgrimesstatic const char * 1501573Srgrimesrangematch(pattern, test, flags) 1511573Srgrimes const char *pattern; 15219132Sache char test; 15319132Sache int flags; 1541573Srgrimes{ 1551573Srgrimes int negate, ok; 1561573Srgrimes char c, c2; 1571573Srgrimes 1581573Srgrimes /* 1591573Srgrimes * A bracket expression starting with an unquoted circumflex 1601573Srgrimes * character produces unspecified results (IEEE 1003.2-1992, 1611573Srgrimes * 3.13.2). This implementation treats it like '!', for 1621573Srgrimes * consistency with the regular expression syntax. 1631573Srgrimes * J.T. Conklin (conklin@ngai.kaleida.com) 1641573Srgrimes */ 16517141Sjkh if ( (negate = (*pattern == '!' || *pattern == '^')) ) 1661573Srgrimes ++pattern; 1678870Srgrimes 16819132Sache if (flags & FNM_CASEFOLD) 16919132Sache test = tolower((unsigned char)test); 17019059Swosch 1711573Srgrimes for (ok = 0; (c = *pattern++) != ']';) { 1721573Srgrimes if (c == '\\' && !(flags & FNM_NOESCAPE)) 1731573Srgrimes c = *pattern++; 1741573Srgrimes if (c == EOS) 1751573Srgrimes return (NULL); 17619059Swosch 17719132Sache if (flags & FNM_CASEFOLD) 17819132Sache c = tolower((unsigned char)c); 17919059Swosch 1808870Srgrimes if (*pattern == '-' 1811573Srgrimes && (c2 = *(pattern+1)) != EOS && c2 != ']') { 1821573Srgrimes pattern += 2; 1831573Srgrimes if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 1841573Srgrimes c2 = *pattern++; 1851573Srgrimes if (c2 == EOS) 1861573Srgrimes return (NULL); 18719059Swosch 18819132Sache if (flags & FNM_CASEFOLD) 18919132Sache c2 = tolower((unsigned char)c2); 19019059Swosch 19124632Sache if (__collate_load_error ? 19224632Sache c <= test && test <= c2 : 19324632Sache __collate_range_cmp(c, test) <= 0 19424632Sache && __collate_range_cmp(test, c2) <= 0 19517533Sache ) 1961573Srgrimes ok = 1; 1971573Srgrimes } else if (c == test) 1981573Srgrimes ok = 1; 1991573Srgrimes } 2001573Srgrimes return (ok == negate ? NULL : pattern); 2011573Srgrimes} 202