fnmatch.c revision 19276
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) 961573Srgrimes return (strchr(string, '/') == NULL ? 971573Srgrimes 0 : FNM_NOMATCH); 981573Srgrimes else 991573Srgrimes return (0); 1001573Srgrimes else if (c == '/' && flags & FNM_PATHNAME) { 1011573Srgrimes if ((string = strchr(string, '/')) == NULL) 1021573Srgrimes return (FNM_NOMATCH); 1031573Srgrimes break; 1041573Srgrimes } 1051573Srgrimes 1061573Srgrimes /* General case, use recursion. */ 1071573Srgrimes while ((test = *string) != EOS) { 1081573Srgrimes if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 1091573Srgrimes return (0); 1101573Srgrimes if (test == '/' && flags & FNM_PATHNAME) 1111573Srgrimes break; 1121573Srgrimes ++string; 1131573Srgrimes } 1141573Srgrimes return (FNM_NOMATCH); 1151573Srgrimes case '[': 1161573Srgrimes if (*string == EOS) 1171573Srgrimes return (FNM_NOMATCH); 1181573Srgrimes if (*string == '/' && flags & FNM_PATHNAME) 1191573Srgrimes return (FNM_NOMATCH); 1201573Srgrimes if ((pattern = 1211573Srgrimes rangematch(pattern, *string, flags)) == NULL) 1221573Srgrimes return (FNM_NOMATCH); 1231573Srgrimes ++string; 1241573Srgrimes break; 1251573Srgrimes case '\\': 1261573Srgrimes if (!(flags & FNM_NOESCAPE)) { 1271573Srgrimes if ((c = *pattern++) == EOS) { 1281573Srgrimes c = '\\'; 1291573Srgrimes --pattern; 1301573Srgrimes } 1311573Srgrimes } 1321573Srgrimes /* FALLTHROUGH */ 1331573Srgrimes default: 13419059Swosch if (c == *string) 13519059Swosch ; 13619132Sache else if ((flags & FNM_CASEFOLD) && 13719132Sache (tolower((unsigned char)c) == 13819132Sache tolower((unsigned char)*string))) 13919059Swosch ; 14019059Swosch else 1411573Srgrimes return (FNM_NOMATCH); 14219059Swosch string++; 1431573Srgrimes break; 1441573Srgrimes } 1451573Srgrimes /* NOTREACHED */ 1461573Srgrimes} 1471573Srgrimes 1481573Srgrimesstatic const char * 1491573Srgrimesrangematch(pattern, test, flags) 1501573Srgrimes const char *pattern; 15119132Sache char test; 15219132Sache int flags; 1531573Srgrimes{ 1541573Srgrimes int negate, ok; 1551573Srgrimes char c, c2; 1561573Srgrimes 1571573Srgrimes /* 1581573Srgrimes * A bracket expression starting with an unquoted circumflex 1591573Srgrimes * character produces unspecified results (IEEE 1003.2-1992, 1601573Srgrimes * 3.13.2). This implementation treats it like '!', for 1611573Srgrimes * consistency with the regular expression syntax. 1621573Srgrimes * J.T. Conklin (conklin@ngai.kaleida.com) 1631573Srgrimes */ 16417141Sjkh if ( (negate = (*pattern == '!' || *pattern == '^')) ) 1651573Srgrimes ++pattern; 1668870Srgrimes 16719132Sache if (flags & FNM_CASEFOLD) 16819132Sache test = tolower((unsigned char)test); 16919059Swosch 1701573Srgrimes for (ok = 0; (c = *pattern++) != ']';) { 1711573Srgrimes if (c == '\\' && !(flags & FNM_NOESCAPE)) 1721573Srgrimes c = *pattern++; 1731573Srgrimes if (c == EOS) 1741573Srgrimes return (NULL); 17519059Swosch 17619132Sache if (flags & FNM_CASEFOLD) 17719132Sache c = tolower((unsigned char)c); 17819059Swosch 1798870Srgrimes if (*pattern == '-' 1801573Srgrimes && (c2 = *(pattern+1)) != EOS && c2 != ']') { 1811573Srgrimes pattern += 2; 1821573Srgrimes if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 1831573Srgrimes c2 = *pattern++; 1841573Srgrimes if (c2 == EOS) 1851573Srgrimes return (NULL); 18619059Swosch 18719132Sache if (flags & FNM_CASEFOLD) 18819132Sache c2 = tolower((unsigned char)c2); 18919059Swosch 19019276Sache if ( __collate_range_cmp(c, test) <= 0 19119276Sache && __collate_range_cmp(test, c2) <= 0 19217533Sache ) 1931573Srgrimes ok = 1; 1941573Srgrimes } else if (c == test) 1951573Srgrimes ok = 1; 1961573Srgrimes } 1971573Srgrimes return (ok == negate ? NULL : pattern); 1981573Srgrimes} 199