| 1 | /* Copyright (C) 1991, 1992 Free Software Foundation, Inc. |
| 2 | |
| 3 | This library is free software; you can redistribute it and/or |
| 4 | modify it under the terms of the GNU Library General Public License as |
| 5 | published by the Free Software Foundation; either version 2 of the |
| 6 | License, or (at your option) any later version. |
| 7 | |
| 8 | This library is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | Library General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU Library General Public |
| 14 | License along with this library; see the file COPYING.LIB. If |
| 15 | not, write to the Free Software Foundation, Inc., 675 Mass Ave, |
| 16 | Cambridge, MA 02139, USA. */ |
| 17 | |
| 18 | #include <errno.h> |
| 19 | #include <fnmatch.h> |
| 20 | |
| 21 | #if !defined(__GNU_LIBRARY__) && !defined(STDC_HEADERS) |
| 22 | extern int errno; |
| 23 | #endif |
| 24 | |
| 25 | /* Match STRING against the filename pattern PATTERN, returning zero if |
| 26 | it matches, nonzero if not. */ |
| 27 | int |
| 28 | fnmatch (pattern, string, flags) |
| 29 | const char *pattern; |
| 30 | const char *string; |
| 31 | int flags; |
| 32 | { |
| 33 | register const char *p = pattern, *n = string; |
| 34 | register char c; |
| 35 | |
| 36 | if ((flags & ~__FNM_FLAGS) != 0) |
| 37 | { |
| 38 | errno = EINVAL; |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | while ((c = *p++) != '\0') |
| 43 | { |
| 44 | switch (c) |
| 45 | { |
| 46 | case '?': |
| 47 | if (*n == '\0') |
| 48 | return FNM_NOMATCH; |
| 49 | else if ((flags & FNM_PATHNAME) && *n == '/') |
| 50 | return FNM_NOMATCH; |
| 51 | else if ((flags & FNM_PERIOD) && *n == '.' && |
| 52 | (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/'))) |
| 53 | return FNM_NOMATCH; |
| 54 | break; |
| 55 | |
| 56 | case '\\': |
| 57 | if (!(flags & FNM_NOESCAPE)) |
| 58 | c = *p++; |
| 59 | if (*n != c) |
| 60 | return FNM_NOMATCH; |
| 61 | break; |
| 62 | |
| 63 | case '*': |
| 64 | if ((flags & FNM_PERIOD) && *n == '.' && |
| 65 | (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/'))) |
| 66 | return FNM_NOMATCH; |
| 67 | |
| 68 | for (c = *p++; c == '?' || c == '*'; c = *p++, ++n) |
| 69 | if (((flags & FNM_PATHNAME) && *n == '/') || |
| 70 | (c == '?' && *n == '\0')) |
| 71 | return FNM_NOMATCH; |
| 72 | |
| 73 | if (c == '\0') |
| 74 | return 0; |
| 75 | |
| 76 | { |
| 77 | char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c; |
| 78 | for (--p; *n != '\0'; ++n) |
| 79 | if ((c == '[' || *n == c1) && |
| 80 | fnmatch (p, n, flags & ~FNM_PERIOD) == 0) |
| 81 | return 0; |
| 82 | return FNM_NOMATCH; |
| 83 | } |
| 84 | |
| 85 | case '[': |
| 86 | { |
| 87 | /* Nonzero if the sense of the character class is inverted. */ |
| 88 | register int not; |
| 89 | |
| 90 | if (*n == '\0') |
| 91 | return FNM_NOMATCH; |
| 92 | |
| 93 | if ((flags & FNM_PERIOD) && *n == '.' && |
| 94 | (n == string || ((flags & FNM_PATHNAME) && n[-1] == '/'))) |
| 95 | return FNM_NOMATCH; |
| 96 | |
| 97 | not = (*p == '!' || *p == '^'); |
| 98 | if (not) |
| 99 | ++p; |
| 100 | |
| 101 | c = *p++; |
| 102 | for (;;) |
| 103 | { |
| 104 | register char cstart = c, cend = c; |
| 105 | |
| 106 | if (!(flags & FNM_NOESCAPE) && c == '\\') |
| 107 | cstart = cend = *p++; |
| 108 | |
| 109 | if (c == '\0') |
| 110 | /* [ (unterminated) loses. */ |
| 111 | return FNM_NOMATCH; |
| 112 | |
| 113 | c = *p++; |
| 114 | |
| 115 | if ((flags & FNM_PATHNAME) && c == '/') |
| 116 | /* [/] can never match. */ |
| 117 | return FNM_NOMATCH; |
| 118 | |
| 119 | if (c == '-' && *p != ']') |
| 120 | { |
| 121 | cend = *p++; |
| 122 | if (!(flags & FNM_NOESCAPE) && cend == '\\') |
| 123 | cend = *p++; |
| 124 | if (cend == '\0') |
| 125 | return FNM_NOMATCH; |
| 126 | c = *p++; |
| 127 | } |
| 128 | |
| 129 | if (*n >= cstart && *n <= cend) |
| 130 | goto matched; |
| 131 | |
| 132 | if (c == ']') |
| 133 | break; |
| 134 | } |
| 135 | if (!not) |
| 136 | return FNM_NOMATCH; |
| 137 | break; |
| 138 | |
| 139 | matched:; |
| 140 | /* Skip the rest of the [...] that already matched. */ |
| 141 | while (c != ']') |
| 142 | { |
| 143 | if (c == '\0') |
| 144 | /* [... (unterminated) loses. */ |
| 145 | return FNM_NOMATCH; |
| 146 | |
| 147 | c = *p++; |
| 148 | if (!(flags & FNM_NOESCAPE) && c == '\\') |
| 149 | /* 1003.2d11 is unclear if this is right. %%% */ |
| 150 | ++p; |
| 151 | } |
| 152 | if (not) |
| 153 | return FNM_NOMATCH; |
| 154 | } |
| 155 | break; |
| 156 | |
| 157 | default: |
| 158 | if (c != *n) |
| 159 | return FNM_NOMATCH; |
| 160 | } |
| 161 | |
| 162 | ++n; |
| 163 | } |
| 164 | |
| 165 | if (*n == '\0') |
| 166 | return 0; |
| 167 | |
| 168 | if ((flags & FNM_LEADING_DIR) && *n == '/') |
| 169 | /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ |
| 170 | return 0; |
| 171 | |
| 172 | return FNM_NOMATCH; |
| 173 | } |