|
| 1 | +/* $NetBSD: fnmatch.c,v 1.26 2014/10/12 22:32:33 christos 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 | +/* |
| 36 | + * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. |
| 37 | + * Compares a filename or pathname to a pattern. |
| 38 | + */ |
| 39 | + |
| 40 | +#include <ctype.h> |
| 41 | +#include "fnmatch.h" |
| 42 | +#include <string.h> |
| 43 | + |
| 44 | +#define EOS '\0' |
| 45 | + |
| 46 | +static inline int foldcase(int ch, int flags) |
| 47 | +{ |
| 48 | + |
| 49 | + if ((flags & FNM_CASEFOLD) != 0 && isupper(ch)) |
| 50 | + return tolower(ch); |
| 51 | + return ch; |
| 52 | +} |
| 53 | + |
| 54 | +#define FOLDCASE(ch, flags) foldcase((unsigned char)(ch), (flags)) |
| 55 | + |
| 56 | +static const char * rangematch(const char *pattern, int test, int flags) |
| 57 | +{ |
| 58 | + int negate, ok, need; |
| 59 | + char c, c2; |
| 60 | + |
| 61 | + if (pattern == NULL) |
| 62 | + { |
| 63 | + return NULL; |
| 64 | + } |
| 65 | + |
| 66 | + /* |
| 67 | + * A bracket expression starting with an unquoted circumflex |
| 68 | + * character produces unspecified results (IEEE 1003.2-1992, |
| 69 | + * 3.13.2). This implementation treats it like '!', for |
| 70 | + * consistency with the regular expression syntax. |
| 71 | + * J.T. Conklin ([email protected]) |
| 72 | + */ |
| 73 | + if ((negate = (*pattern == '!' || *pattern == '^')) != 0) |
| 74 | + ++pattern; |
| 75 | + |
| 76 | + need = 1; |
| 77 | + for (ok = 0; (c = FOLDCASE(*pattern++, flags)) != ']' || need;) { |
| 78 | + need = 0; |
| 79 | + if (c == '/') |
| 80 | + return (void *)-1; |
| 81 | + if (c == '\\' && !(flags & FNM_NOESCAPE)) |
| 82 | + c = FOLDCASE(*pattern++, flags); |
| 83 | + if (c == EOS) |
| 84 | + return NULL; |
| 85 | + if (*pattern == '-' |
| 86 | + && (c2 = FOLDCASE(*(pattern + 1), flags)) != EOS && |
| 87 | + c2 != ']') { |
| 88 | + pattern += 2; |
| 89 | + if (c2 == '\\' && !(flags & FNM_NOESCAPE)) |
| 90 | + c2 = FOLDCASE(*pattern++, flags); |
| 91 | + if (c2 == EOS) |
| 92 | + return NULL; |
| 93 | + if (c <= test && test <= c2) |
| 94 | + ok = 1; |
| 95 | + } else if (c == test) |
| 96 | + ok = 1; |
| 97 | + } |
| 98 | + return ok == negate ? NULL : pattern; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +static int fnmatchx(const char *pattern, const char *string, int flags, size_t recursion) |
| 103 | +{ |
| 104 | + const char *stringstart, *r; |
| 105 | + char c, test; |
| 106 | + |
| 107 | + if ((pattern == NULL) || (string == NULL)) |
| 108 | + { |
| 109 | + return FNM_NOMATCH; |
| 110 | + } |
| 111 | + |
| 112 | + if (recursion-- == 0) |
| 113 | + return FNM_NORES; |
| 114 | + |
| 115 | + for (stringstart = string;;) { |
| 116 | + switch (c = FOLDCASE(*pattern++, flags)) { |
| 117 | + case EOS: |
| 118 | + if ((flags & FNM_LEADING_DIR) && *string == '/') |
| 119 | + return 0; |
| 120 | + return *string == EOS ? 0 : 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 | + ++string; |
| 131 | + break; |
| 132 | + case '*': |
| 133 | + c = FOLDCASE(*pattern, flags); |
| 134 | + /* Collapse multiple stars. */ |
| 135 | + while (c == '*') |
| 136 | + c = FOLDCASE(*++pattern, flags); |
| 137 | + |
| 138 | + if (*string == '.' && (flags & FNM_PERIOD) && |
| 139 | + (string == stringstart || |
| 140 | + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) |
| 141 | + return FNM_NOMATCH; |
| 142 | + |
| 143 | + /* Optimize for pattern with * at end or before /. */ |
| 144 | + if (c == EOS) { |
| 145 | + if (flags & FNM_PATHNAME) |
| 146 | + return (flags & FNM_LEADING_DIR) || |
| 147 | + strchr(string, '/') == NULL ? |
| 148 | + 0 : FNM_NOMATCH; |
| 149 | + else |
| 150 | + return 0; |
| 151 | + } else if (c == '/' && flags & FNM_PATHNAME) { |
| 152 | + if ((string = strchr(string, '/')) == NULL) |
| 153 | + return FNM_NOMATCH; |
| 154 | + break; |
| 155 | + } |
| 156 | + |
| 157 | + /* General case, use recursion. */ |
| 158 | + while ((test = FOLDCASE(*string, flags)) != EOS) { |
| 159 | + int e; |
| 160 | + switch ((e = fnmatchx(pattern, string, |
| 161 | + flags & ~FNM_PERIOD, recursion))) { |
| 162 | + case FNM_NOMATCH: |
| 163 | + break; |
| 164 | + default: |
| 165 | + return e; |
| 166 | + } |
| 167 | + if (test == '/' && flags & FNM_PATHNAME) |
| 168 | + break; |
| 169 | + ++string; |
| 170 | + } |
| 171 | + return FNM_NOMATCH; |
| 172 | + case '[': |
| 173 | + if (*string == EOS) |
| 174 | + return FNM_NOMATCH; |
| 175 | + if (*string == '/' && flags & FNM_PATHNAME) |
| 176 | + return FNM_NOMATCH; |
| 177 | + if ((r = rangematch(pattern, |
| 178 | + FOLDCASE(*string, flags), flags)) == NULL) |
| 179 | + return FNM_NOMATCH; |
| 180 | + if (r == (void *)-1) { |
| 181 | + if (*string != '[') |
| 182 | + return FNM_NOMATCH; |
| 183 | + } else |
| 184 | + pattern = r; |
| 185 | + ++string; |
| 186 | + break; |
| 187 | + case '\\': |
| 188 | + if (!(flags & FNM_NOESCAPE)) { |
| 189 | + if ((c = FOLDCASE(*pattern++, flags)) == EOS) { |
| 190 | + c = '\0'; |
| 191 | + --pattern; |
| 192 | + } |
| 193 | + } |
| 194 | + /* FALLTHROUGH */ |
| 195 | + default: |
| 196 | + if (c != FOLDCASE(*string++, flags)) |
| 197 | + return FNM_NOMATCH; |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + /* NOTREACHED */ |
| 202 | +} |
| 203 | + |
| 204 | +int fnmatch(const char *pattern, const char *string, int flags) |
| 205 | +{ |
| 206 | + return fnmatchx(pattern, string, flags, 64); |
| 207 | +} |
| 208 | + |
0 commit comments