bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* $OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 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. All advertising materials mentioning features or use of this software |
| 19 | * must display the following acknowledgement: |
| 20 | * This product includes software developed by the University of |
| 21 | * California, Berkeley and its contributors. |
| 22 | * 4. Neither the name of the University nor the names of its contributors |
| 23 | * may be used to endorse or promote products derived from this software |
| 24 | * without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 36 | * SUCH DAMAGE. |
| 37 | */ |
| 38 | |
| 39 | #if defined(LIBC_SCCS) && !defined(lint) |
| 40 | #if 0 |
| 41 | static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; |
| 42 | #else |
| 43 | static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $"; |
| 44 | #endif |
| 45 | #endif /* LIBC_SCCS and not lint */ |
| 46 | |
| 47 | /* |
| 48 | * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. |
| 49 | * Compares a filename or pathname to a pattern. |
| 50 | */ |
| 51 | |
| 52 | #include <config.h> |
| 53 | |
| 54 | #include <stdio.h> |
| 55 | |
| 56 | #ifdef STDC_HEADERS |
| 57 | # include <string.h> |
| 58 | #endif |
| 59 | |
| 60 | #ifdef HAVE_CTYPE_H |
| 61 | # include <ctype.h> |
| 62 | #endif |
| 63 | |
| 64 | #include <compat.h> |
| 65 | |
| 66 | |
| 67 | #define EOS '\0' |
| 68 | |
| 69 | #define RANGE_MATCH 1 |
| 70 | #define RANGE_NOMATCH 0 |
| 71 | #define RANGE_ERROR (-1) |
| 72 | |
| 73 | #ifdef NO_IBM_COMPILER_HORKAGE |
| 74 | static int rangematch (const char *, char, int, char **); |
| 75 | #else |
| 76 | static int rangematch (); |
| 77 | #endif |
| 78 | |
| 79 | int |
| 80 | fnmatch(pattern, string, flags) |
| 81 | const char *pattern, *string; |
| 82 | int flags; |
| 83 | { |
| 84 | const char *stringstart; |
| 85 | char *newp; |
| 86 | char c, test; |
| 87 | |
| 88 | for (stringstart = string;;) |
| 89 | switch (c = *pattern++) { |
| 90 | case EOS: |
| 91 | if ((flags & FNM_LEADING_DIR) && *string == '/') |
| 92 | return (0); |
| 93 | return (*string == EOS ? 0 : FNM_NOMATCH); |
| 94 | case '?': |
| 95 | if (*string == EOS) |
| 96 | return (FNM_NOMATCH); |
| 97 | if (*string == '/' && (flags & FNM_PATHNAME)) |
| 98 | return (FNM_NOMATCH); |
| 99 | if (*string == '.' && (flags & FNM_PERIOD) && |
| 100 | (string == stringstart || |
| 101 | ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) |
| 102 | return (FNM_NOMATCH); |
| 103 | ++string; |
| 104 | break; |
| 105 | case '*': |
| 106 | c = *pattern; |
| 107 | /* Collapse multiple stars. */ |
| 108 | while (c == '*') |
| 109 | c = *++pattern; |
| 110 | |
| 111 | if (*string == '.' && (flags & FNM_PERIOD) && |
| 112 | (string == stringstart || |
| 113 | ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) |
| 114 | return (FNM_NOMATCH); |
| 115 | |
| 116 | /* Optimize for pattern with * at end or before /. */ |
| 117 | if (c == EOS) { |
| 118 | if (flags & FNM_PATHNAME) |
| 119 | return ((flags & FNM_LEADING_DIR) || |
| 120 | strchr(string, '/') == NULL ? |
| 121 | 0 : FNM_NOMATCH); |
| 122 | else |
| 123 | return (0); |
| 124 | } else if (c == '/' && (flags & FNM_PATHNAME)) { |
| 125 | if ((string = strchr(string, '/')) == NULL) |
| 126 | return (FNM_NOMATCH); |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | /* General case, use recursion. */ |
| 131 | while ((test = *string) != EOS) { |
| 132 | if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) |
| 133 | return (0); |
| 134 | if (test == '/' && (flags & FNM_PATHNAME)) |
| 135 | break; |
| 136 | ++string; |
| 137 | } |
| 138 | return (FNM_NOMATCH); |
| 139 | case '[': |
| 140 | if (*string == EOS) |
| 141 | return (FNM_NOMATCH); |
| 142 | if (*string == '/' && (flags & FNM_PATHNAME)) |
| 143 | return (FNM_NOMATCH); |
| 144 | if (*string == '.' && (flags & FNM_PERIOD) && |
| 145 | (string == stringstart || |
| 146 | ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) |
| 147 | return (FNM_NOMATCH); |
| 148 | |
| 149 | switch (rangematch(pattern, *string, flags, &newp)) { |
| 150 | case RANGE_ERROR: |
| 151 | /* not a good range, treat as normal text */ |
| 152 | goto normal; |
| 153 | case RANGE_MATCH: |
| 154 | pattern = newp; |
| 155 | break; |
| 156 | case RANGE_NOMATCH: |
| 157 | return (FNM_NOMATCH); |
| 158 | } |
| 159 | ++string; |
| 160 | break; |
| 161 | case '\\': |
| 162 | if (!(flags & FNM_NOESCAPE)) { |
| 163 | if ((c = *pattern++) == EOS) { |
| 164 | c = '\\'; |
| 165 | --pattern; |
| 166 | } |
| 167 | } |
| 168 | /* FALLTHROUGH */ |
| 169 | default: |
| 170 | normal: |
| 171 | if (c != *string && !((flags & FNM_CASEFOLD) && |
| 172 | (tolower((unsigned char)c) == |
| 173 | tolower((unsigned char)*string)))) |
| 174 | return (FNM_NOMATCH); |
| 175 | ++string; |
| 176 | break; |
| 177 | } |
| 178 | /* NOTREACHED */ |
| 179 | } |
| 180 | |
| 181 | static int |
| 182 | rangematch(pattern, test, flags, newp) |
| 183 | const char *pattern; |
| 184 | char test; |
| 185 | int flags; |
| 186 | char **newp; |
| 187 | { |
| 188 | int negate, ok; |
| 189 | char c, c2; |
| 190 | |
| 191 | /* |
| 192 | * A bracket expression starting with an unquoted circumflex |
| 193 | * character produces unspecified results (IEEE 1003.2-1992, |
| 194 | * 3.13.2). This implementation treats it like '!', for |
| 195 | * consistency with the regular expression syntax. |
| 196 | * J.T. Conklin (conklin@ngai.kaleida.com) |
| 197 | */ |
| 198 | if ((negate = (*pattern == '!' || *pattern == '^'))) |
| 199 | ++pattern; |
| 200 | |
| 201 | if (flags & FNM_CASEFOLD) |
| 202 | test = tolower((unsigned char)test); |
| 203 | |
| 204 | /* |
| 205 | * A right bracket shall lose its special meaning and represent |
| 206 | * itself in a bracket expression if it occurs first in the list. |
| 207 | * -- POSIX.2 2.8.3.2 |
| 208 | */ |
| 209 | ok = 0; |
| 210 | c = *pattern++; |
| 211 | do { |
| 212 | if (c == '\\' && !(flags & FNM_NOESCAPE)) |
| 213 | c = *pattern++; |
| 214 | if (c == EOS) |
| 215 | return (RANGE_ERROR); |
| 216 | if (c == '/' && (flags & FNM_PATHNAME)) |
| 217 | return (RANGE_NOMATCH); |
| 218 | if ((flags & FNM_CASEFOLD)) |
| 219 | c = tolower((unsigned char)c); |
| 220 | if (*pattern == '-' |
| 221 | && (c2 = *(pattern+1)) != EOS && c2 != ']') { |
| 222 | pattern += 2; |
| 223 | if (c2 == '\\' && !(flags & FNM_NOESCAPE)) |
| 224 | c2 = *pattern++; |
| 225 | if (c2 == EOS) |
| 226 | return (RANGE_ERROR); |
| 227 | if (flags & FNM_CASEFOLD) |
| 228 | c2 = tolower((unsigned char)c2); |
| 229 | if (c <= test && test <= c2) |
| 230 | ok = 1; |
| 231 | } else if (c == test) |
| 232 | ok = 1; |
| 233 | } while ((c = *pattern++) != ']'); |
| 234 | |
| 235 | *newp = (char *)pattern; |
| 236 | return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); |
| 237 | } |