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 <fnmatch.h> 28 29 struct testcase { 30 const char *pattern; 31 const char *string; 32 int flags; 33 int result; 34 } testcases[] = { 35 { "", "", 0, 0 }, 36 { "a", "a", 0, 0 }, 37 { "a", "b", 0, FNM_NOMATCH }, 38 { "a", "A", 0, FNM_NOMATCH }, 39 { "*", "a", 0, 0 }, 40 { "*", "aa", 0, 0 }, 41 { "*a", "a", 0, 0 }, 42 { "*a", "b", 0, FNM_NOMATCH }, 43 { "*a*", "b", 0, FNM_NOMATCH }, 44 { "*a*b*", "ab", 0, 0 }, 45 { "*a*b*", "qaqbq", 0, 0 }, 46 { "*a*bb*", "qaqbqbbq", 0, 0 }, 47 { "*a*bc*", "qaqbqbcq", 0, 0 }, 48 { "*a*bb*", "qaqbqbb", 0, 0 }, 49 { "*a*bc*", "qaqbqbc", 0, 0 }, 50 { "*a*bb", "qaqbqbb", 0, 0 }, 51 { "*a*bc", "qaqbqbc", 0, 0 }, 52 { "*a*bb", "qaqbqbbq", 0, FNM_NOMATCH }, 53 { "*a*bc", "qaqbqbcq", 0, FNM_NOMATCH }, 54 { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaa", 0, FNM_NOMATCH }, 55 { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaa", 0, 0 }, 56 { "*a*a*a*a*a*a*a*a*a*a*", "aaaaaaaaaaa", 0, 0 }, 57 { ".*.*.*.*.*.*.*.*.*.*", ".........", 0, FNM_NOMATCH }, 58 { ".*.*.*.*.*.*.*.*.*.*", "..........", 0, 0 }, 59 { ".*.*.*.*.*.*.*.*.*.*", "...........", 0, 0 }, 60 { "*?*?*?*?*?*?*?*?*?*?*", "123456789", 0, FNM_NOMATCH }, 61 { "??????????*", "123456789", 0, FNM_NOMATCH }, 62 { "*??????????", "123456789", 0, FNM_NOMATCH }, 63 { "*?*?*?*?*?*?*?*?*?*?*", "1234567890", 0, 0 }, 64 { "??????????*", "1234567890", 0, 0 }, 65 { "*??????????", "1234567890", 0, 0 }, 66 { "*?*?*?*?*?*?*?*?*?*?*", "12345678901", 0, 0 }, 67 { "??????????*", "12345678901", 0, 0 }, 68 { "*??????????", "12345678901", 0, 0 }, 69 { "[x]", "x", 0, 0 }, 70 { "[*]", "*", 0, 0 }, 71 { "[?]", "?", 0, 0 }, 72 { "[", "[", 0, 0 }, 73 { "[[]", "[", 0, 0 }, 74 { "[[]", "x", 0, FNM_NOMATCH }, 75 { "[*]", "", 0, FNM_NOMATCH }, 76 { "[*]", "x", 0, FNM_NOMATCH }, 77 { "[?]", "x", 0, FNM_NOMATCH }, 78 { "*[*]*", "foo*foo", 0, 0 }, 79 { "*[*]*", "foo", 0, FNM_NOMATCH }, 80 { "[0-9]", "0", 0, 0 }, 81 { "[0-9]", "5", 0, 0 }, 82 { "[0-9]", "9", 0, 0 }, 83 { "[0-9]", "/", 0, FNM_NOMATCH }, 84 { "[0-9]", ":", 0, FNM_NOMATCH }, 85 { "[0-9]", "*", 0, FNM_NOMATCH }, 86 { "[!0-9]", "0", 0, FNM_NOMATCH }, 87 { "[!0-9]", "5", 0, FNM_NOMATCH }, 88 { "[!0-9]", "9", 0, FNM_NOMATCH }, 89 { "[!0-9]", "/", 0, 0 }, 90 { "[!0-9]", ":", 0, 0 }, 91 { "[!0-9]", "*", 0, 0 }, 92 { "*[0-9]", "a0", 0, 0 }, 93 { "*[0-9]", "a5", 0, 0 }, 94 { "*[0-9]", "a9", 0, 0 }, 95 { "*[0-9]", "a/", 0, FNM_NOMATCH }, 96 { "*[0-9]", "a:", 0, FNM_NOMATCH }, 97 { "*[0-9]", "a*", 0, FNM_NOMATCH }, 98 { "*[!0-9]", "a0", 0, FNM_NOMATCH }, 99 { "*[!0-9]", "a5", 0, FNM_NOMATCH }, 100 { "*[!0-9]", "a9", 0, FNM_NOMATCH }, 101 { "*[!0-9]", "a/", 0, 0 }, 102 { "*[!0-9]", "a:", 0, 0 }, 103 { "*[!0-9]", "a*", 0, 0 }, 104 { "*[0-9]", "a00", 0, 0 }, 105 { "*[0-9]", "a55", 0, 0 }, 106 { "*[0-9]", "a99", 0, 0 }, 107 { "*[0-9]", "a0a0", 0, 0 }, 108 { "*[0-9]", "a5a5", 0, 0 }, 109 { "*[0-9]", "a9a9", 0, 0 }, 110 { "\\*", "*", 0, 0 }, 111 { "\\?", "?", 0, 0 }, 112 { "\\[x]", "[x]", 0, 0 }, 113 { "\\[", "[", 0, 0 }, 114 { "\\\\", "\\", 0, 0 }, 115 { "*\\**", "foo*foo", 0, 0 }, 116 { "*\\**", "foo", 0, FNM_NOMATCH }, 117 { "*\\\\*", "foo\\foo", 0, 0 }, 118 { "*\\\\*", "foo", 0, FNM_NOMATCH }, 119 { "\\(", "(", 0, 0 }, 120 { "\\a", "a", 0, 0 }, 121 { "\\*", "a", 0, FNM_NOMATCH }, 122 { "\\?", "a", 0, FNM_NOMATCH }, 123 { "\\*", "\\*", 0, FNM_NOMATCH }, 124 { "\\?", "\\?", 0, FNM_NOMATCH }, 125 { "\\[x]", "\\[x]", 0, FNM_NOMATCH }, 126 { "\\[x]", "\\x", 0, FNM_NOMATCH }, 127 { "\\[", "\\[", 0, FNM_NOMATCH }, 128 { "\\(", "\\(", 0, FNM_NOMATCH }, 129 { "\\a", "\\a", 0, FNM_NOMATCH }, 130 { "\\", "\\", 0, FNM_NOMATCH }, 131 { "\\", "", 0, FNM_NOMATCH }, 132 { "\\*", "\\*", FNM_NOESCAPE, 0 }, 133 { "\\?", "\\?", FNM_NOESCAPE, 0 }, 134 { "\\", "\\", FNM_NOESCAPE, 0 }, 135 { "\\\\", "\\", FNM_NOESCAPE, FNM_NOMATCH }, 136 { "\\\\", "\\\\", FNM_NOESCAPE, 0 }, 137 { "*\\*", "foo\\foo", FNM_NOESCAPE, 0 }, 138 { "*\\*", "foo", FNM_NOESCAPE, FNM_NOMATCH }, 139 { "*", ".", FNM_PERIOD, FNM_NOMATCH }, 140 { "?", ".", FNM_PERIOD, FNM_NOMATCH }, 141 { ".*", ".", 0, 0 }, 142 { ".*", "..", 0, 0 }, 143 { ".*", ".a", 0, 0 }, 144 { "[0-9]", ".", FNM_PERIOD, FNM_NOMATCH }, 145 { "a*", "a.", 0, 0 }, 146 { "a/a", "a/a", FNM_PATHNAME, 0 }, 147 { "a/*", "a/a", FNM_PATHNAME, 0 }, 148 { "*/a", "a/a", FNM_PATHNAME, 0 }, 149 { "*/*", "a/a", FNM_PATHNAME, 0 }, 150 { "a*b/*", "abbb/x", FNM_PATHNAME, 0 }, 151 { "a*b/*", "abbb/.x", FNM_PATHNAME, 0 }, 152 { "*", "a/a", FNM_PATHNAME, FNM_NOMATCH }, 153 { "*/*", "a/a/a", FNM_PATHNAME, FNM_NOMATCH }, 154 { "b/*", "b/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, 155 { "b*/*", "a/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, 156 { "b/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, 157 { "b*/.*", "b/.x", FNM_PATHNAME | FNM_PERIOD, 0 }, 158 { "a", "A", FNM_CASEFOLD, 0 }, 159 { "A", "a", FNM_CASEFOLD, 0 }, 160 { "[a]", "A", FNM_CASEFOLD, 0 }, 161 { "[A]", "a", FNM_CASEFOLD, 0 }, 162 { "a", "b", FNM_CASEFOLD, FNM_NOMATCH }, 163 { "a", "a/b", FNM_PATHNAME, FNM_NOMATCH }, 164 { "*", "a/b", FNM_PATHNAME, FNM_NOMATCH }, 165 { "*b", "a/b", FNM_PATHNAME, FNM_NOMATCH }, 166 { "a", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 167 { "*", "a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 168 { "*", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 169 { "*a", ".a/b", FNM_PATHNAME | FNM_LEADING_DIR, 0 }, 170 { "*", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, 171 { "*a", ".a/b", FNM_PATHNAME | FNM_PERIOD | FNM_LEADING_DIR, FNM_NOMATCH }, 172 { "a*b/*", "abbb/.x", FNM_PATHNAME | FNM_PERIOD, FNM_NOMATCH }, 173 }; 174