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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $";
35 #endif /* LIBC_SCCS and not lint */
38 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
39 * Compares a filename or pathname to a pattern.
49 # ifdef HAVE_STRINGS_H
52 #endif /* HAVE_STRING_H */
55 #include "emul/fnmatch.h"
61 #define RANGE_NOMATCH 0
62 #define RANGE_ERROR (-1)
64 static int rangematch __P((const char *, char, int, char **));
67 fnmatch(pattern, string, flags)
68 const char *pattern, *string;
71 const char *stringstart;
75 for (stringstart = string;;)
76 switch (c = *pattern++) {
78 if (ISSET(flags, FNM_LEADING_DIR) && *string == '/')
80 return (*string == EOS ? 0 : FNM_NOMATCH);
84 if (*string == '/' && ISSET(flags, FNM_PATHNAME))
86 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
87 (string == stringstart ||
88 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
94 /* Collapse multiple stars. */
98 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
99 (string == stringstart ||
100 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
101 return (FNM_NOMATCH);
103 /* Optimize for pattern with * at end or before /. */
105 if (ISSET(flags, FNM_PATHNAME))
106 return (ISSET(flags, FNM_LEADING_DIR) ||
107 strchr(string, '/') == NULL ?
111 } else if (c == '/' && ISSET(flags, FNM_PATHNAME)) {
112 if ((string = strchr(string, '/')) == NULL)
113 return (FNM_NOMATCH);
117 /* General case, use recursion. */
118 while ((test = *string) != EOS) {
119 if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
121 if (test == '/' && ISSET(flags, FNM_PATHNAME))
125 return (FNM_NOMATCH);
128 return (FNM_NOMATCH);
129 if (*string == '/' && ISSET(flags, FNM_PATHNAME))
130 return (FNM_NOMATCH);
131 if (*string == '.' && ISSET(flags, FNM_PERIOD) &&
132 (string == stringstart ||
133 (ISSET(flags, FNM_PATHNAME) && *(string - 1) == '/')))
134 return (FNM_NOMATCH);
136 switch (rangematch(pattern, *string, flags, &newp)) {
138 /* not a good range, treat as normal text */
144 return (FNM_NOMATCH);
149 if (!ISSET(flags, FNM_NOESCAPE)) {
150 if ((c = *pattern++) == EOS) {
158 if (c != *string && !(ISSET(flags, FNM_CASEFOLD) &&
159 (tolower((unsigned char)c) ==
160 tolower((unsigned char)*string))))
161 return (FNM_NOMATCH);
170 rangematch(const char *pattern, char test, int flags, char **newp)
172 rangematch(pattern, test, flags, newp)
183 * A bracket expression starting with an unquoted circumflex
184 * character produces unspecified results (IEEE 1003.2-1992,
185 * 3.13.2). This implementation treats it like '!', for
186 * consistency with the regular expression syntax.
187 * J.T. Conklin (conklin@ngai.kaleida.com)
189 if ((negate = (*pattern == '!' || *pattern == '^')))
192 if (ISSET(flags, FNM_CASEFOLD))
193 test = tolower((unsigned char)test);
196 * A right bracket shall lose its special meaning and represent
197 * itself in a bracket expression if it occurs first in the list.
203 if (c == '\\' && !ISSET(flags, FNM_NOESCAPE))
206 return (RANGE_ERROR);
207 if (c == '/' && ISSET(flags, FNM_PATHNAME))
208 return (RANGE_NOMATCH);
209 if (ISSET(flags, FNM_CASEFOLD))
210 c = tolower((unsigned char)c);
212 && (c2 = *(pattern+1)) != EOS && c2 != ']') {
214 if (c2 == '\\' && !ISSET(flags, FNM_NOESCAPE))
217 return (RANGE_ERROR);
218 if (ISSET(flags, FNM_CASEFOLD))
219 c2 = tolower((unsigned char)c2);
220 if (c <= test && test <= c2)
222 } else if (c == test)
224 } while ((c = *pattern++) != ']');
226 *newp = (char *)pattern;
227 return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);