fnmatch.c revision 19059
1/*
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Guido van Rossum.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static char sccsid[] = "@(#)fnmatch.c	8.2 (Berkeley) 4/16/94";
39#endif /* LIBC_SCCS and not lint */
40
41/*
42 * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
43 * Compares a filename or pathname to a pattern.
44 */
45
46#include <ctype.h>
47#include <fnmatch.h>
48#include <locale.h>
49#include <string.h>
50#include <stdio.h>
51
52#define	EOS	'\0'
53
54static const char *rangematch __P((const char *, int, int));
55
56int
57fnmatch(pattern, string, flags)
58	const char *pattern, *string;
59	int flags;
60{
61	const char *stringstart;
62	char c, test;
63
64	for (stringstart = string;;)
65		switch (c = *pattern++) {
66		case EOS:
67			return (*string == EOS ? 0 : FNM_NOMATCH);
68		case '?':
69			if (*string == EOS)
70				return (FNM_NOMATCH);
71			if (*string == '/' && (flags & FNM_PATHNAME))
72				return (FNM_NOMATCH);
73			if (*string == '.' && (flags & FNM_PERIOD) &&
74			    (string == stringstart ||
75			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
76				return (FNM_NOMATCH);
77			++string;
78			break;
79		case '*':
80			c = *pattern;
81			/* Collapse multiple stars. */
82			while (c == '*')
83				c = *++pattern;
84
85			if (*string == '.' && (flags & FNM_PERIOD) &&
86			    (string == stringstart ||
87			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
88				return (FNM_NOMATCH);
89
90			/* Optimize for pattern with * at end or before /. */
91			if (c == EOS)
92				if (flags & FNM_PATHNAME)
93					return (strchr(string, '/') == NULL ?
94					    0 : FNM_NOMATCH);
95				else
96					return (0);
97			else if (c == '/' && flags & FNM_PATHNAME) {
98				if ((string = strchr(string, '/')) == NULL)
99					return (FNM_NOMATCH);
100				break;
101			}
102
103			/* General case, use recursion. */
104			while ((test = *string) != EOS) {
105				if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
106					return (0);
107				if (test == '/' && flags & FNM_PATHNAME)
108					break;
109				++string;
110			}
111			return (FNM_NOMATCH);
112		case '[':
113			if (*string == EOS)
114				return (FNM_NOMATCH);
115			if (*string == '/' && flags & FNM_PATHNAME)
116				return (FNM_NOMATCH);
117			if ((pattern =
118			    rangematch(pattern, *string, flags)) == NULL)
119				return (FNM_NOMATCH);
120			++string;
121			break;
122		case '\\':
123			if (!(flags & FNM_NOESCAPE)) {
124				if ((c = *pattern++) == EOS) {
125					c = '\\';
126					--pattern;
127				}
128			}
129			/* FALLTHROUGH */
130		default:
131			if (c == *string)
132				;
133			else if ((flags & FNM_ICASE) &&
134				 (tolower(c) == tolower(*string)))
135				;
136			else
137				return (FNM_NOMATCH);
138			string++;
139			break;
140		}
141	/* NOTREACHED */
142}
143
144static const char *
145rangematch(pattern, test, flags)
146	const char *pattern;
147	int test, flags;
148{
149	int negate, ok;
150	char c, c2;
151
152	/*
153	 * A bracket expression starting with an unquoted circumflex
154	 * character produces unspecified results (IEEE 1003.2-1992,
155	 * 3.13.2).  This implementation treats it like '!', for
156	 * consistency with the regular expression syntax.
157	 * J.T. Conklin (conklin@ngai.kaleida.com)
158	 */
159	if ( (negate = (*pattern == '!' || *pattern == '^')) )
160		++pattern;
161
162	if (flags & FNM_ICASE)
163		test = tolower(test);
164
165	for (ok = 0; (c = *pattern++) != ']';) {
166		if (c == '\\' && !(flags & FNM_NOESCAPE))
167			c = *pattern++;
168		if (c == EOS)
169			return (NULL);
170
171		if (flags & FNM_ICASE)
172			c = tolower(c);
173
174		if (*pattern == '-'
175		    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
176			pattern += 2;
177			if (c2 == '\\' && !(flags & FNM_NOESCAPE))
178				c2 = *pattern++;
179			if (c2 == EOS)
180				return (NULL);
181
182			if (flags & FNM_ICASE)
183				c2 = tolower(c2);
184
185			if (   collate_range_cmp(c, test) <= 0
186			    && collate_range_cmp(test, c2) <= 0
187			   )
188				ok = 1;
189		} else if (c == test)
190			ok = 1;
191	}
192	return (ok == negate ? NULL : pattern);
193}
194