1 /*- 2 * Copyright (c) 2010 Jilles Tjoelker 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <fnmatch.h> 31 32 struct testcase { 33 const char *pattern; 34 const char *string; 35 int flags; 36 int result; 37 } testcases[] = { 38 { "", "", 0, 0 }, 39 { "a", "a", 0, 0 }, 40 { "a", "b", 0, FNM_NOMATCH }, 41 { "a", "A", 0, FNM_NOMATCH }, 42 { "*", "a", 0, 0 }, 43 { "*", "aa", 0, 0 }, 44 { "*a", "a", 0, 0 }, 45 { "*a", "b", 0, FNM_NOMATCH }, 46 { "*a*", "b", 0, FNM_NOMATCH }, 47 { "*a*b*", "ab", 0, 0 }, 48 { "*a*b*", "qaqbq", 0, 0 }, 49 { "*a*bb*", "qaqbqbbq", 0, 0 }, 50 { "*a*bc*", "qaqbqbcq", 0, 0 }, 51 { "*a*bb*", "qaqbqbb", 0, 0 }, 52 { "*a*bc*", "qaqbqbc", 0, 0 }, 53 { "*a*bb", "qaqbqbb", 0, 0 }, 54 { "*a*bc", "qaqbqbc", 0, 0 }, 55 { "*a*bb", "qaqbqbbq", 0, FNM_NOMATCH }, 56 { "*a*bc", "qaqbqbcq", 0, FNM_NOMATCH }, 57 { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaa", 0, FNM_NOMATCH }, 58 { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaa", 0, 0 }, 59 { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaaa", 0, 0 }, 60 { ".*.*.*.*.*.*.*.*.*.*", ".........", 0, FNM_NOMATCH }, 61 { ".*.*.*.*.*.*.*.*.*.*", "..........", 0, 0 }, 62 { ".*.*.*.*.*.*.*.*.*.*", "...........", 0, 0 }, 63 { "*?*?*?*?*?*?*?*?*?*?*", "123456789", 0, FNM_NOMATCH }, 64 { "??????????*", "123456789", 0, FNM_NOMATCH }, 65 { "*??????????", "123456789", 0, FNM_NOMATCH }, 66 { "*?*?*?*?*?*?*?*?*?*?*", "1234567890", 0, 0 }, 67 { "??????????*", "1234567890", 0, 0 }, 68 { "*??????????", "1234567890", 0, 0 }, 69 { "*?*?*?*?*?*?*?*?*?*?*", "12345678901", 0, 0 }, 70 { "??????????*", "12345678901", 0, 0 }, 71 { "*??????????", "12345678901", 0, 0 }, 72 { "[x]", "x", 0, 0 }, 73 { "[*]", "*", 0, 0 }, 74 { "[?]", "?", 0, 0 }, 75 { "[", "[", 0, 0 }, 76 { "[[]", "[", 0, 0 }, 77 { "[[]", "x", 0, FNM_NOMATCH }, 78 { "[*]", "", 0, FNM_NOMATCH }, 79 { "[*]", "x", 0, FNM_NOMATCH }, 80 { "[?]", "x", 0, FNM_NOMATCH }, 81 { "*[*]*", "foo*foo", 0, 0 }, 82 { "*[*]*", "foo", 0, FNM_NOMATCH }, 83 { "[0-9]", "0", 0, 0 }, 84 { "[0-9]", "5", 0, 0 }, 85 { "[0-9]", "9", 0, 0 }, 86 { "[0-9]", "/", 0, FNM_NOMATCH }, 87 { "[0-9]", ":", 0, FNM_NOMATCH }, 88 { "[0-9]", "*", 0, FNM_NOMATCH }, 89 { "[!0-9]", "0", 0, FNM_NOMATCH }, 90 { "[!0-9]", "5", 0, FNM_NOMATCH }, 91 { "[!0-9]", "9", 0, FNM_NOMATCH }, 92 { "[!0-9]", "/", 0, 0 }, 93 { "[!0-9]", ":", 0, 0 }, 94 { "[!0-9]", "*", 0, 0 }, 95 { "*[0-9]", "a0", 0, 0 }, 96 { "*[0-9]", "a5", 0, 0 }, 97 { "*[0-9]", "a9", 0, 0 }, 98 { "*[0-9]", "a/", 0, FNM_NOMATCH }, 99 { "*[0-9]", "a:", 0, FNM_NOMATCH }, 100 { "*[0-9]", "a*", 0, FNM_NOMATCH }, 101 { "*[!0-9]", "a0", 0, FNM_NOMATCH }, 102 { "*[!0-9]", "a5", 0, FNM_NOMATCH }, 103 { "*[!0-9]", "a9", 0, FNM_NOMATCH }, 104 { "*[!0-9]", "a/", 0, 0 }, 105 { "*[!0-9]", "a:", 0, 0 }, 106 { "*[!0-9]", "a*", 0, 0 }, 107 { "*[0-9]", "a00", 0, 0 }, 108 { "*[0-9]", "a55", 0, 0 }, 109 { "*[0-9]", "a99", 0, 0 }, 110 { "*[0-9]", "a0a0", 0, 0 }, 111 { "*[0-9]", "a5a5", 0, 0 }, 112 { "*[0-9]", "a9a9", 0, 0 }, 113 { "\\*", "*", 0, 0 }, 114 { "\\?", "?", 0, 0 }, 115 { "\\[x]", "[x]", 0, 0 }, 116 { "\\[", "[", 0, 0 }, 117 { "\\\\", "\\", 0, 0 }, 118 { "*\\**", "foo*foo", 0, 0 }, 119 { "*\\**", "foo", 0, FNM_NOMATCH }, 120 { "*\\\\*", "foo\\foo", 0, 0 }, 121 { "*\\\\*", "foo", 0, FNM_NOMATCH }, 122 { "\\(", "(", 0, 0 }, 123 { "\\a", "a", 0, 0 }, 124 { "\\*", "a", 0, FNM_NOMATCH }, 125 { "\\?", "a", 0, FNM_NOMATCH }, 126 { "\\*", "\\*", 0, FNM_NOMATCH }, 127 { "\\?", "\\?", 0, FNM_NOMATCH }, 128 { "\\[x]", "\\[x]", 0, FNM_NOMATCH }, 129 { "\\[x]", "\\x", 0, FNM_NOMATCH }, 130 { "\\[", "\\[", 0, FNM_NOMATCH }, 131 { "\\(", "\\(", 0, FNM_NOMATCH }, 132 { "\\a", "\\a", 0, FNM_NOMATCH }, 133 { "\\", "\\", 0, FNM_NOMATCH }, 134 { "\\", "", 0, 0 }, 135 { "\\*", "\\*", FNM_NOESCAPE, 0 }, 136 { "\\?", "\\?", FNM_NOESCAPE, 0 }, 137 { "\\", "\\", FNM_NOESCAPE, 0 }, 138 { "\\\\", "\\", FNM_NOESCAPE, FNM_NOMATCH }, 139 { "\\\\", "\\\\", FNM_NOESCAPE, 0 }, 140 { "*\\*", "foo\\foo", FNM_NOESCAPE, 0 }, 141 { "*\\*", "foo", FNM_NOESCAPE, FNM_NOMATCH }, 142 { "*", ".", FNM_PERIOD, FNM_NOMATCH }, 143 { "?", ".", FNM_PERIOD, FNM_NOMATCH }, 144 { ".*", ".", 0, 0 }, 145 { ".*", "..", 0, 0 }, 146 { ".*", ".a", 0, 0 }, 147 { "[0-9]", ".", FNM_PERIOD, FNM_NOMATCH }, 148 { "a*", "a.", 0, 0 }, 149 { "a/a", "a/a", FNM_PATHNAME, 0 }, 150 { "a/*", "a/a", FNM_PATHNAME, 0 }, 151 { "*/a", "a/a", FNM_PATHNAME, 0 }, 152 { "*/*", "a/a", FNM_PATHNAME, 0 }, 153 { "a*b/*", "abbb/x", FNM_PATHNAME, 0 }, 154 { "a*b/*", "abbb/.x", FNM_PATHNAME, 0 }, 155 { "*", "a/a", FNM_PATHNAME, FNM_NOMATCH }, 156 { "*/*", "a/a/a", FNM_PATHNAME, FNM_NOMATCH }, 157 { "b/*", "b/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, 158 { "b*/*", "a/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, 159 { "b/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, 160 { "b*/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, 161 { "a", "A", FNM_CASEFOLD, 0 }, 162 { "A", "a", FNM_CASEFOLD, 0 }, 163 { "[a]", "A", FNM_CASEFOLD, 0 }, 164 { "[A]", "a", FNM_CASEFOLD, 0 }, 165 { "a", "b", FNM_CASEFOLD, FNM_NOMATCH }, 166 { "a", "a/b", FNM_PATHNAME, FNM_NOMATCH }, 167 { "*", "a/b", FNM_PATHNAME, FNM_NOMATCH }, 168 { "*b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, 169 { "a", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 170 { "*", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 171 { "*", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 172 { "*a", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 173 { "*", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, 174 { "*a", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, 175 { "a*b/*", "abbb/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, 176 }; 177