2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94";
39 #endif /* LIBC_SCCS and not lint */
42 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
43 * Compares a filename or pathname to a pattern.
46 #include "apr_private.h"
48 #include "apr_fnmatch.h"
57 static const char *rangematch(const char *, int, int);
59 APR_DECLARE(apr_status_t) apr_fnmatch(const char *pattern, const char *string, int flags)
61 const char *stringstart;
64 for (stringstart = string;;) {
65 switch (c = *pattern++) {
67 return (*string == EOS ? APR_SUCCESS : APR_FNM_NOMATCH);
70 return (APR_FNM_NOMATCH);
72 if (*string == '/' && (flags & APR_FNM_PATHNAME)) {
73 return (APR_FNM_NOMATCH);
75 if (*string == '.' && (flags & APR_FNM_PERIOD) &&
76 (string == stringstart ||
77 ((flags & APR_FNM_PATHNAME) && *(string - 1) == '/'))) {
78 return (APR_FNM_NOMATCH);
84 /* Collapse multiple stars. */
89 if (*string == '.' && (flags & APR_FNM_PERIOD) &&
90 (string == stringstart ||
91 ((flags & APR_FNM_PATHNAME) && *(string - 1) == '/'))) {
92 return (APR_FNM_NOMATCH);
95 /* Optimize for pattern with * at end or before /. */
97 if (flags & APR_FNM_PATHNAME) {
98 return (strchr(string, '/') == NULL ? APR_SUCCESS : APR_FNM_NOMATCH);
101 return (APR_SUCCESS);
104 else if (c == '/' && flags & APR_FNM_PATHNAME) {
105 if ((string = strchr(string, '/')) == NULL) {
106 return (APR_FNM_NOMATCH);
111 /* General case, use recursion. */
112 while ((test = *string) != EOS) {
113 if (!apr_fnmatch(pattern, string, flags & ~APR_FNM_PERIOD)) {
114 return (APR_SUCCESS);
116 if (test == '/' && flags & APR_FNM_PATHNAME) {
121 return (APR_FNM_NOMATCH);
123 if (*string == EOS) {
124 return (APR_FNM_NOMATCH);
126 if (*string == '/' && flags & APR_FNM_PATHNAME) {
127 return (APR_FNM_NOMATCH);
129 if (*string == '.' && (flags & APR_FNM_PERIOD) &&
130 (string == stringstart ||
131 ((flags & APR_FNM_PATHNAME) && *(string - 1) == '/'))) {
132 return (APR_FNM_NOMATCH);
134 if ((pattern = rangematch(pattern, *string, flags)) == NULL) {
135 return (APR_FNM_NOMATCH);
140 if (!(flags & APR_FNM_NOESCAPE)) {
141 if ((c = *pattern++) == EOS) {
148 if (flags & APR_FNM_CASE_BLIND) {
149 if (apr_tolower(c) != apr_tolower(*string)) {
150 return (APR_FNM_NOMATCH);
153 else if (c != *string) {
154 return (APR_FNM_NOMATCH);
163 static const char *rangematch(const char *pattern, int test, int flags)
169 * A bracket expression starting with an unquoted circumflex
170 * character produces unspecified results (IEEE 1003.2-1992,
171 * 3.13.2). This implementation treats it like '!', for
172 * consistency with the regular expression syntax.
173 * J.T. Conklin (conklin@ngai.kaleida.com)
175 if ((negate = (*pattern == '!' || *pattern == '^'))) {
179 for (ok = 0; (c = *pattern++) != ']';) {
180 if (c == '\\' && !(flags & APR_FNM_NOESCAPE)) {
186 if (*pattern == '-' && (c2 = *(pattern + 1)) != EOS && c2 != ']') {
188 if (c2 == '\\' && !(flags & APR_FNM_NOESCAPE)) {
194 if ((c <= test && test <= c2)
195 || ((flags & APR_FNM_CASE_BLIND)
196 && ((apr_tolower(c) <= apr_tolower(test))
197 && (apr_tolower(test) <= apr_tolower(c2))))) {
202 || ((flags & APR_FNM_CASE_BLIND)
203 && (apr_tolower(c) == apr_tolower(test)))) {
207 return (ok == negate ? NULL : pattern);
211 /* This function is an Apache addition */
212 /* return non-zero if pattern has any glob chars in it */
213 APR_DECLARE(int) apr_fnmatch_test(const char *pattern)
225 if (*pattern++ == '\0') {
230 case '[': /* '[' is only a glob if it has a matching ']' */
246 APR_DECLARE(int) apr_is_fnmatch(const char *pattern)
248 return apr_fnmatch_test(pattern);