fnmatch.c revision 19132
1/* 2 * Copyright (c) 1989, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Guido van Rossum. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; 39#endif /* LIBC_SCCS and not lint */ 40 41/* 42 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. 43 * Compares a filename or pathname to a pattern. 44 */ 45 46#include <ctype.h> 47#include <fnmatch.h> 48#include <locale.h> 49#include <string.h> 50#include <stdio.h> 51 52#define EOS '\0' 53 54static const char *rangematch __P((const char *, char, int)); 55 56int 57fnmatch(pattern, string, flags) 58 const char *pattern, *string; 59 int flags; 60{ 61 const char *stringstart; 62 char c, test; 63 64 for (stringstart = string;;) 65 switch (c = *pattern++) { 66 case EOS: 67 if ((flags & FNM_LEADING_DIR) && *string == '/') 68 return (0); 69 return (*string == EOS ? 0 : FNM_NOMATCH); 70 case '?': 71 if (*string == EOS) 72 return (FNM_NOMATCH); 73 if (*string == '/' && (flags & FNM_PATHNAME)) 74 return (FNM_NOMATCH); 75 if (*string == '.' && (flags & FNM_PERIOD) && 76 (string == stringstart || 77 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 78 return (FNM_NOMATCH); 79 ++string; 80 break; 81 case '*': 82 c = *pattern; 83 /* Collapse multiple stars. */ 84 while (c == '*') 85 c = *++pattern; 86 87 if (*string == '.' && (flags & FNM_PERIOD) && 88 (string == stringstart || 89 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 90 return (FNM_NOMATCH); 91 92 /* Optimize for pattern with * at end or before /. */ 93 if (c == EOS) 94 if (flags & FNM_PATHNAME) 95 return (strchr(string, '/') == NULL ? 96 0 : FNM_NOMATCH); 97 else 98 return (0); 99 else if (c == '/' && flags & FNM_PATHNAME) { 100 if ((string = strchr(string, '/')) == NULL) 101 return (FNM_NOMATCH); 102 break; 103 } 104 105 /* General case, use recursion. */ 106 while ((test = *string) != EOS) { 107 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 108 return (0); 109 if (test == '/' && flags & FNM_PATHNAME) 110 break; 111 ++string; 112 } 113 return (FNM_NOMATCH); 114 case '[': 115 if (*string == EOS) 116 return (FNM_NOMATCH); 117 if (*string == '/' && flags & FNM_PATHNAME) 118 return (FNM_NOMATCH); 119 if ((pattern = 120 rangematch(pattern, *string, flags)) == NULL) 121 return (FNM_NOMATCH); 122 ++string; 123 break; 124 case '\\': 125 if (!(flags & FNM_NOESCAPE)) { 126 if ((c = *pattern++) == EOS) { 127 c = '\\'; 128 --pattern; 129 } 130 } 131 /* FALLTHROUGH */ 132 default: 133 if (c == *string) 134 ; 135 else if ((flags & FNM_CASEFOLD) && 136 (tolower((unsigned char)c) == 137 tolower((unsigned char)*string))) 138 ; 139 else 140 return (FNM_NOMATCH); 141 string++; 142 break; 143 } 144 /* NOTREACHED */ 145} 146 147static const char * 148rangematch(pattern, test, flags) 149 const char *pattern; 150 char test; 151 int flags; 152{ 153 int negate, ok; 154 char c, c2; 155 156 /* 157 * A bracket expression starting with an unquoted circumflex 158 * character produces unspecified results (IEEE 1003.2-1992, 159 * 3.13.2). This implementation treats it like '!', for 160 * consistency with the regular expression syntax. 161 * J.T. Conklin (conklin@ngai.kaleida.com) 162 */ 163 if ( (negate = (*pattern == '!' || *pattern == '^')) ) 164 ++pattern; 165 166 if (flags & FNM_CASEFOLD) 167 test = tolower((unsigned char)test); 168 169 for (ok = 0; (c = *pattern++) != ']';) { 170 if (c == '\\' && !(flags & FNM_NOESCAPE)) 171 c = *pattern++; 172 if (c == EOS) 173 return (NULL); 174 175 if (flags & FNM_CASEFOLD) 176 c = tolower((unsigned char)c); 177 178 if (*pattern == '-' 179 && (c2 = *(pattern+1)) != EOS && c2 != ']') { 180 pattern += 2; 181 if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 182 c2 = *pattern++; 183 if (c2 == EOS) 184 return (NULL); 185 186 if (flags & FNM_CASEFOLD) 187 c2 = tolower((unsigned char)c2); 188 189 if ( collate_range_cmp(c, test) <= 0 190 && collate_range_cmp(test, c2) <= 0 191 ) 192 ok = 1; 193 } else if (c == test) 194 ok = 1; 195 } 196 return (ok == negate ? NULL : pattern); 197} 198