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 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 <fnmatch.h> 47 #include <locale.h> 48 #include <string.h> 49 50 #define EOS '\0' 51 52 static const char *rangematch __P((const char *, int, int)); 53 54 int 55 fnmatch(pattern, string, flags) 56 const char *pattern, *string; 57 int flags; 58 { 59 const char *stringstart; 60 char c, test; 61 62 for (stringstart = string;;) 63 switch (c = *pattern++) { 64 case EOS: 65 return (*string == EOS ? 0 : FNM_NOMATCH); 66 case '?': 67 if (*string == EOS) 68 return (FNM_NOMATCH); 69 if (*string == '/' && (flags & FNM_PATHNAME)) 70 return (FNM_NOMATCH); 71 if (*string == '.' && (flags & FNM_PERIOD) && 72 (string == stringstart || 73 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 74 return (FNM_NOMATCH); 75 ++string; 76 break; 77 case '*': 78 c = *pattern; 79 /* Collapse multiple stars. */ 80 while (c == '*') 81 c = *++pattern; 82 83 if (*string == '.' && (flags & FNM_PERIOD) && 84 (string == stringstart || 85 ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) 86 return (FNM_NOMATCH); 87 88 /* Optimize for pattern with * at end or before /. */ 89 if (c == EOS) 90 if (flags & FNM_PATHNAME) 91 return (strchr(string, '/') == NULL ? 92 0 : FNM_NOMATCH); 93 else 94 return (0); 95 else if (c == '/' && flags & FNM_PATHNAME) { 96 if ((string = strchr(string, '/')) == NULL) 97 return (FNM_NOMATCH); 98 break; 99 } 100 101 /* General case, use recursion. */ 102 while ((test = *string) != EOS) { 103 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) 104 return (0); 105 if (test == '/' && flags & FNM_PATHNAME) 106 break; 107 ++string; 108 } 109 return (FNM_NOMATCH); 110 case '[': 111 if (*string == EOS) 112 return (FNM_NOMATCH); 113 if (*string == '/' && flags & FNM_PATHNAME) 114 return (FNM_NOMATCH); 115 if ((pattern = 116 rangematch(pattern, *string, flags)) == NULL) 117 return (FNM_NOMATCH); 118 ++string; 119 break; 120 case '\\': 121 if (!(flags & FNM_NOESCAPE)) { 122 if ((c = *pattern++) == EOS) { 123 c = '\\'; 124 --pattern; 125 } 126 } 127 /* FALLTHROUGH */ 128 default: 129 if (c != *string++) 130 return (FNM_NOMATCH); 131 break; 132 } 133 /* NOTREACHED */ 134 } 135 136 static const char * 137 rangematch(pattern, test, flags) 138 const char *pattern; 139 int test, flags; 140 { 141 int negate, ok; 142 char c, c2; 143 144 /* 145 * A bracket expression starting with an unquoted circumflex 146 * character produces unspecified results (IEEE 1003.2-1992, 147 * 3.13.2). This implementation treats it like '!', for 148 * consistency with the regular expression syntax. 149 * J.T. Conklin (conklin@ngai.kaleida.com) 150 */ 151 if ( (negate = (*pattern == '!' || *pattern == '^')) ) 152 ++pattern; 153 154 for (ok = 0; (c = *pattern++) != ']';) { 155 if (c == '\\' && !(flags & FNM_NOESCAPE)) 156 c = *pattern++; 157 if (c == EOS) 158 return (NULL); 159 if (*pattern == '-' 160 && (c2 = *(pattern+1)) != EOS && c2 != ']') { 161 pattern += 2; 162 if (c2 == '\\' && !(flags & FNM_NOESCAPE)) 163 c2 = *pattern++; 164 if (c2 == EOS) 165 return (NULL); 166 if ( collate_range_cmp(c, test) <= 0 167 && collate_range_cmp(test, c2) <= 0 168 ) 169 ok = 1; 170 } else if (c == test) 171 ok = 1; 172 } 173 return (ok == negate ? NULL : pattern); 174 } 175