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 rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
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.
53 # ifdef HAVE_STRINGS_H
56 #endif /* HAVE_STRING_H */
59 #include "emul/fnmatch.h"
65 #define RANGE_NOMATCH 0
66 #define RANGE_ERROR (-1)
68 static int rangematch __P((const char *, char, int, char **));
71 fnmatch(pattern, string, flags)
72 const char *pattern, *string;
75 const char *stringstart;
79 for (stringstart = string;;)
80 switch (c = *pattern++) {
82 if ((flags & FNM_LEADING_DIR) && *string == '/')
84 return (*string == EOS ? 0 : FNM_NOMATCH);
88 if (*string == '/' && (flags & FNM_PATHNAME))
90 if (*string == '.' && (flags & FNM_PERIOD) &&
91 (string == stringstart ||
92 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
98 /* Collapse multiple stars. */
102 if (*string == '.' && (flags & FNM_PERIOD) &&
103 (string == stringstart ||
104 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
105 return (FNM_NOMATCH);
107 /* Optimize for pattern with * at end or before /. */
109 if (flags & FNM_PATHNAME)
110 return ((flags & FNM_LEADING_DIR) ||
111 strchr(string, '/') == NULL ?
115 } else if (c == '/' && (flags & FNM_PATHNAME)) {
116 if ((string = strchr(string, '/')) == NULL)
117 return (FNM_NOMATCH);
121 /* General case, use recursion. */
122 while ((test = *string) != EOS) {
123 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
125 if (test == '/' && (flags & FNM_PATHNAME))
129 return (FNM_NOMATCH);
132 return (FNM_NOMATCH);
133 if (*string == '/' && (flags & FNM_PATHNAME))
134 return (FNM_NOMATCH);
135 if (*string == '.' && (flags & FNM_PERIOD) &&
136 (string == stringstart ||
137 ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
138 return (FNM_NOMATCH);
140 switch (rangematch(pattern, *string, flags, &newp)) {
142 /* not a good range, treat as normal text */
148 return (FNM_NOMATCH);
153 if (!(flags & FNM_NOESCAPE)) {
154 if ((c = *pattern++) == EOS) {
162 if (c != *string && !((flags & FNM_CASEFOLD) &&
163 (tolower((unsigned char)c) ==
164 tolower((unsigned char)*string))))
165 return (FNM_NOMATCH);
174 rangematch(const char *pattern, char test, int flags, char **newp)
176 rangematch(pattern, test, flags, newp)
187 * A bracket expression starting with an unquoted circumflex
188 * character produces unspecified results (IEEE 1003.2-1992,
189 * 3.13.2). This implementation treats it like '!', for
190 * consistency with the regular expression syntax.
191 * J.T. Conklin (conklin@ngai.kaleida.com)
193 if ((negate = (*pattern == '!' || *pattern == '^')))
196 if (flags & FNM_CASEFOLD)
197 test = tolower((unsigned char)test);
200 * A right bracket shall lose its special meaning and represent
201 * itself in a bracket expression if it occurs first in the list.
207 if (c == '\\' && !(flags & FNM_NOESCAPE))
210 return (RANGE_ERROR);
211 if (c == '/' && (flags & FNM_PATHNAME))
212 return (RANGE_NOMATCH);
213 if ((flags & FNM_CASEFOLD))
214 c = tolower((unsigned char)c);
216 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
218 if (c2 == '\\' && !(flags & FNM_NOESCAPE))
221 return (RANGE_ERROR);
222 if (flags & FNM_CASEFOLD)
223 c2 = tolower((unsigned char)c2);
224 if (c <= test && test <= c2)
226 } else if (c == test)
228 } while ((c = *pattern++) != ']');
230 *newp = (char *)pattern;
231 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);