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