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) 38 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; 39 #endif /* LIBC_SCCS and not lint */ 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. 45 * Compares a filename or pathname to a pattern. 46 */ 47 48 #include <ctype.h> 49 #include <fnmatch.h> 50 #include <string.h> 51 #include <stdio.h> 52 53 #include "collate.h" 54 55 #define EOS '\0' 56 57 #define RANGE_MATCH 1 58 #define RANGE_NOMATCH 0 59 #define RANGE_ERROR (-1) 60 61 static int rangematch(const char *, char, int, char **); 62 63 int 64 fnmatch(pattern, string, flags) 65 const char *pattern, *string; 66 int flags; 67 { 68 const char *stringstart; 69 char *newp; 70 char c, test; 71 72 for (stringstart = string;;) 73 switch (c = *pattern++) { 74 case EOS: 75 if ((flags & FNM_LEADING_DIR) && *string == '/') 76 return (0); 77 return (*string == EOS ? 0 : FNM_NOMATCH); 78 case '?': 79 if (*string == EOS) 80 return (FNM_NOMATCH); 81 if (*string == '/' && (flags & FNM_PATHNAME)) 82 return (FNM_NOMATCH); 83 if (*string == '.' && (flags & FNM_PERIOD) && 84 (string == stringstart || 85 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 86 return (FNM_NOMATCH); 87 ++string; 88 break; 89 case '*': 90 c = *pattern; 91 /* Collapse multiple stars. */ 92 while (c == '*') 93 c = *++pattern; 94 95 if (*string == '.' && (flags & FNM_PERIOD) && 96 (string == stringstart || 97 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 98 return (FNM_NOMATCH); 99 100 /* Optimize for pattern with * at end or before /. */ 101 if (c == EOS) 102 if (flags & FNM_PATHNAME) 103 return ((flags & FNM_LEADING_DIR) || 104 strchr(string, '/') == NULL ? 105 0 : FNM_NOMATCH); 106 else 107 return (0); 108 else if (c == '/' && flags & FNM_PATHNAME) { 109 if ((string = strchr(string, '/')) == NULL) 110 return (FNM_NOMATCH); 111 break; 112 } 113 114 /* General case, use recursion. */ 115 while ((test = *string) != EOS) { 116 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 117 return (0); 118 if (test == '/' && flags & FNM_PATHNAME) 119 break; 120 ++string; 121 } 122 return (FNM_NOMATCH); 123 case '[': 124 if (*string == EOS) 125 return (FNM_NOMATCH); 126 if (*string == '/' && (flags & FNM_PATHNAME)) 127 return (FNM_NOMATCH); 128 if (*string == '.' && (flags & FNM_PERIOD) && 129 (string == stringstart || 130 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 131 return (FNM_NOMATCH); 132 133 switch (rangematch(pattern, *string, flags, &newp)) { 134 case RANGE_ERROR: 135 goto norm; 136 case RANGE_MATCH: 137 pattern = newp; 138 break; 139 case RANGE_NOMATCH: 140 return (FNM_NOMATCH); 141 } 142 ++string; 143 break; 144 case '\\': 145 if (!(flags & FNM_NOESCAPE)) { 146 if ((c = *pattern++) == EOS) { 147 c = '\\'; 148 --pattern; 149 } 150 } 151 /* FALLTHROUGH */ 152 default: 153 norm: 154 if (c == *string) 155 ; 156 else if ((flags & FNM_CASEFOLD) && 157 (tolower((unsigned char)c) == 158 tolower((unsigned char)*string))) 159 ; 160 else 161 return (FNM_NOMATCH); 162 string++; 163 break; 164 } 165 /* NOTREACHED */ 166 } 167 168 static int 169 rangematch(pattern, test, flags, newp) 170 const char *pattern; 171 char test; 172 int flags; 173 char **newp; 174 { 175 int negate, ok; 176 char c, c2; 177 178 /* 179 * A bracket expression starting with an unquoted circumflex 180 * character produces unspecified results (IEEE 1003.2-1992, 181 * 3.13.2). This implementation treats it like '!', for 182 * consistency with the regular expression syntax. 183 * J.T. Conklin (conklin@ngai.kaleida.com) 184 */ 185 if ( (negate = (*pattern == '!' || *pattern == '^')) ) 186 ++pattern; 187 188 if (flags & FNM_CASEFOLD) 189 test = tolower((unsigned char)test); 190 191 /* 192 * A right bracket shall lose its special meaning and represent 193 * itself in a bracket expression if it occurs first in the list. 194 * -- POSIX.2 2.8.3.2 195 */ 196 ok = 0; 197 c = *pattern++; 198 do { 199 if (c == '\\' && !(flags & FNM_NOESCAPE)) 200 c = *pattern++; 201 if (c == EOS) 202 return (RANGE_ERROR); 203 204 if (c == '/' && (flags & FNM_PATHNAME)) 205 return (RANGE_NOMATCH); 206 207 if (flags & FNM_CASEFOLD) 208 c = tolower((unsigned char)c); 209 210 if (*pattern == '-' 211 && (c2 = *(pattern+1)) != EOS && c2 != ']') { 212 pattern += 2; 213 if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 214 c2 = *pattern++; 215 if (c2 == EOS) 216 return (RANGE_ERROR); 217 218 if (flags & FNM_CASEFOLD) 219 c2 = tolower((unsigned char)c2); 220 221 if (__collate_load_error ? 222 c <= test && test <= c2 : 223 __collate_range_cmp(c, test) <= 0 224 && __collate_range_cmp(test, c2) <= 0 225 ) 226 ok = 1; 227 } else if (c == test) 228 ok = 1; 229 } while ((c = *pattern++) != ']'); 230 231 *newp = (char *)pattern; 232 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); 233 } 234