2 * Copyright (c) 2010 Jilles Tjoelker
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
39 #include "fnmatch_testcases.h"
42 flags_to_string(int flags)
44 static const int flagvalues[] = { FNM_NOESCAPE, FNM_PATHNAME,
45 FNM_PERIOD, FNM_LEADING_DIR, FNM_CASEFOLD, 0 };
46 static const char flagnames[] = "FNM_NOESCAPE\0FNM_PATHNAME\0FNM_PERIOD\0FNM_LEADING_DIR\0FNM_CASEFOLD\0";
47 static char result[sizeof(flagnames) + 3 * sizeof(int) + 2];
54 for (i = 0; flagvalues[i] != 0; i++) {
56 if (flags & flagvalues[i]) {
61 flags &= ~flagvalues[i];
68 sprintf(p, "%d", flags);
74 ATF_TC_WITHOUT_HEAD(fnmatch_test);
75 ATF_TC_BODY(fnmatch_test, tc)
81 for (i = 0; i < nitems(testcases); i++) {
85 result = fnmatch(t->pattern, t->string, flags);
86 if (result != t->result)
88 if (strchr(t->pattern, '\\') == NULL &&
89 !(flags & FNM_NOESCAPE)) {
90 flags |= FNM_NOESCAPE;
91 result = fnmatch(t->pattern, t->string, flags);
92 if (result != t->result)
96 if (strchr(t->pattern, '\\') != NULL &&
97 strchr(t->string, '\\') == NULL &&
98 t->result == FNM_NOMATCH &&
99 !(flags & (FNM_NOESCAPE | FNM_LEADING_DIR))) {
100 flags |= FNM_NOESCAPE;
101 result = fnmatch(t->pattern, t->string, flags);
102 if (result != t->result)
106 if ((t->string[0] != '.' || t->pattern[0] == '.' ||
107 t->result == FNM_NOMATCH) &&
108 !(flags & (FNM_PATHNAME | FNM_PERIOD))) {
110 result = fnmatch(t->pattern, t->string, flags);
111 if (result != t->result)
115 if ((strchr(t->string, '/') == NULL ||
116 t->result == FNM_NOMATCH) &&
117 !(flags & FNM_PATHNAME)) {
118 flags |= FNM_PATHNAME;
119 result = fnmatch(t->pattern, t->string, flags);
120 if (result != t->result)
124 if ((((t->string[0] != '.' || t->pattern[0] == '.') &&
125 strstr(t->string, "/.") == NULL) ||
126 t->result == FNM_NOMATCH) &&
127 flags & FNM_PATHNAME && !(flags & FNM_PERIOD)) {
129 result = fnmatch(t->pattern, t->string, flags);
130 if (result != t->result)
134 if ((((t->string[0] != '.' || t->pattern[0] == '.') &&
135 strchr(t->string, '/') == NULL) ||
136 t->result == FNM_NOMATCH) &&
137 !(flags & (FNM_PATHNAME | FNM_PERIOD))) {
138 flags |= FNM_PATHNAME | FNM_PERIOD;
139 result = fnmatch(t->pattern, t->string, flags);
140 if (result != t->result)
144 if ((strchr(t->string, '/') == NULL || t->result == 0)
145 && !(flags & FNM_LEADING_DIR)) {
146 flags |= FNM_LEADING_DIR;
147 result = fnmatch(t->pattern, t->string, flags);
148 if (result != t->result)
152 if (t->result == 0 && !(flags & FNM_CASEFOLD)) {
153 flags |= FNM_CASEFOLD;
154 result = fnmatch(t->pattern, t->string, flags);
155 if (result != t->result)
159 if (strchr(t->pattern, '\\') == NULL &&
161 !(flags & (FNM_NOESCAPE | FNM_CASEFOLD))) {
162 flags |= FNM_NOESCAPE | FNM_CASEFOLD;
163 result = fnmatch(t->pattern, t->string, flags);
164 if (result != t->result)
170 ATF_CHECK(result == t->result);
171 if (result == t->result)
172 printf("fnmatch(\"%s\", \"%s\", %s) == %d\n",
173 t->pattern, t->string, flags_to_string(flags), result);
175 printf("fnmatch(\"%s\", \"%s\", %s) != %d (was %d)\n",
176 t->pattern, t->string, flags_to_string(flags),
185 ATF_TP_ADD_TC(tp, fnmatch_test);
187 return (atf_no_error());