fnmatch.c revision 17533
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 461573Srgrimes#include <fnmatch.h> 471573Srgrimes#include <string.h> 4817533Sache#include "collate.h" 491573Srgrimes 501573Srgrimes#define EOS '\0' 511573Srgrimes 521573Srgrimesstatic const char *rangematch __P((const char *, int, int)); 531573Srgrimes 541573Srgrimesint 551573Srgrimesfnmatch(pattern, string, flags) 561573Srgrimes const char *pattern, *string; 571573Srgrimes int flags; 581573Srgrimes{ 591573Srgrimes const char *stringstart; 601573Srgrimes char c, test; 611573Srgrimes 621573Srgrimes for (stringstart = string;;) 631573Srgrimes switch (c = *pattern++) { 641573Srgrimes case EOS: 651573Srgrimes return (*string == EOS ? 0 : FNM_NOMATCH); 661573Srgrimes case '?': 671573Srgrimes if (*string == EOS) 681573Srgrimes return (FNM_NOMATCH); 691573Srgrimes if (*string == '/' && (flags & FNM_PATHNAME)) 701573Srgrimes return (FNM_NOMATCH); 711573Srgrimes if (*string == '.' && (flags & FNM_PERIOD) && 721573Srgrimes (string == stringstart || 731573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 741573Srgrimes return (FNM_NOMATCH); 751573Srgrimes ++string; 761573Srgrimes break; 771573Srgrimes case '*': 781573Srgrimes c = *pattern; 791573Srgrimes /* Collapse multiple stars. */ 801573Srgrimes while (c == '*') 811573Srgrimes c = *++pattern; 821573Srgrimes 831573Srgrimes if (*string == '.' && (flags & FNM_PERIOD) && 841573Srgrimes (string == stringstart || 851573Srgrimes ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 861573Srgrimes return (FNM_NOMATCH); 871573Srgrimes 881573Srgrimes /* Optimize for pattern with * at end or before /. */ 891573Srgrimes if (c == EOS) 901573Srgrimes if (flags & FNM_PATHNAME) 911573Srgrimes return (strchr(string, '/') == NULL ? 921573Srgrimes 0 : FNM_NOMATCH); 931573Srgrimes else 941573Srgrimes return (0); 951573Srgrimes else if (c == '/' && flags & FNM_PATHNAME) { 961573Srgrimes if ((string = strchr(string, '/')) == NULL) 971573Srgrimes return (FNM_NOMATCH); 981573Srgrimes break; 991573Srgrimes } 1001573Srgrimes 1011573Srgrimes /* General case, use recursion. */ 1021573Srgrimes while ((test = *string) != EOS) { 1031573Srgrimes if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 1041573Srgrimes return (0); 1051573Srgrimes if (test == '/' && flags & FNM_PATHNAME) 1061573Srgrimes break; 1071573Srgrimes ++string; 1081573Srgrimes } 1091573Srgrimes return (FNM_NOMATCH); 1101573Srgrimes case '[': 1111573Srgrimes if (*string == EOS) 1121573Srgrimes return (FNM_NOMATCH); 1131573Srgrimes if (*string == '/' && flags & FNM_PATHNAME) 1141573Srgrimes return (FNM_NOMATCH); 1151573Srgrimes if ((pattern = 1161573Srgrimes rangematch(pattern, *string, flags)) == NULL) 1171573Srgrimes return (FNM_NOMATCH); 1181573Srgrimes ++string; 1191573Srgrimes break; 1201573Srgrimes case '\\': 1211573Srgrimes if (!(flags & FNM_NOESCAPE)) { 1221573Srgrimes if ((c = *pattern++) == EOS) { 1231573Srgrimes c = '\\'; 1241573Srgrimes --pattern; 1251573Srgrimes } 1261573Srgrimes } 1271573Srgrimes /* FALLTHROUGH */ 1281573Srgrimes default: 1291573Srgrimes if (c != *string++) 1301573Srgrimes return (FNM_NOMATCH); 1311573Srgrimes break; 1321573Srgrimes } 1331573Srgrimes /* NOTREACHED */ 1341573Srgrimes} 1351573Srgrimes 1361573Srgrimesstatic const char * 1371573Srgrimesrangematch(pattern, test, flags) 1381573Srgrimes const char *pattern; 1391573Srgrimes int test, flags; 1401573Srgrimes{ 1411573Srgrimes int negate, ok; 1421573Srgrimes char c, c2; 1431573Srgrimes 1441573Srgrimes /* 1451573Srgrimes * A bracket expression starting with an unquoted circumflex 1461573Srgrimes * character produces unspecified results (IEEE 1003.2-1992, 1471573Srgrimes * 3.13.2). This implementation treats it like '!', for 1481573Srgrimes * consistency with the regular expression syntax. 1491573Srgrimes * J.T. Conklin (conklin@ngai.kaleida.com) 1501573Srgrimes */ 15117141Sjkh if ( (negate = (*pattern == '!' || *pattern == '^')) ) 1521573Srgrimes ++pattern; 1538870Srgrimes 1541573Srgrimes for (ok = 0; (c = *pattern++) != ']';) { 1551573Srgrimes if (c == '\\' && !(flags & FNM_NOESCAPE)) 1561573Srgrimes c = *pattern++; 1571573Srgrimes if (c == EOS) 1581573Srgrimes return (NULL); 1598870Srgrimes if (*pattern == '-' 1601573Srgrimes && (c2 = *(pattern+1)) != EOS && c2 != ']') { 1611573Srgrimes pattern += 2; 1621573Srgrimes if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 1631573Srgrimes c2 = *pattern++; 1641573Srgrimes if (c2 == EOS) 1651573Srgrimes return (NULL); 16617533Sache if ( __collcmp(c, test) <= 0 16717533Sache && __collcmp(test, c2) <= 0 16817533Sache ) 1691573Srgrimes ok = 1; 1701573Srgrimes } else if (c == test) 1711573Srgrimes ok = 1; 1721573Srgrimes } 1731573Srgrimes return (ok == negate ? NULL : pattern); 1741573Srgrimes} 175