1 /*	$OpenBSD: fnmatch.c,v 1.9 2002/02/19 19:39:36 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Guido van Rossum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #if defined(LIBC_SCCS) && !defined(lint)
40 #if 0
41 static char sccsid[] = "@(#)fnmatch.c	8.2 (Berkeley) 4/16/94";
42 #else
43 static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.9 2002/02/19 19:39:36 millert Exp $";
44 #endif
45 #endif /* LIBC_SCCS and not lint */
46 
47 /*
48  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
49  * Compares a filename or pathname to a pattern.
50  */
51 
52 #include <ctype.h>
53 #include <stdio.h>
54 #include <string.h>
55 #include <fnmatch.h>
56 
57 #define	EOS	'\0'
58 
59 #define	RANGE_MATCH	1
60 #define	RANGE_NOMATCH	0
61 #define	RANGE_ERROR	(-1)
62 
63 static int rangematch(const char *, char, int, char **);
64 
65 int
fnmatch(pattern,string,flags)66 fnmatch(pattern, string, flags)
67 	const char *pattern, *string;
68 	int flags;
69 {
70 	const char *stringstart;
71 	char *newp;
72 	char c, test;
73 
74 	for (stringstart = string;;)
75 		switch (c = *pattern++) {
76 		case EOS:
77 			if ((flags & FNM_LEADING_DIR) && *string == '/')
78 				return (0);
79 			return (*string == EOS ? 0 : FNM_NOMATCH);
80 		case '?':
81 			if (*string == EOS)
82 				return (FNM_NOMATCH);
83 			if (*string == '/' && (flags & FNM_PATHNAME))
84 				return (FNM_NOMATCH);
85 			if (*string == '.' && (flags & FNM_PERIOD) &&
86 			    (string == stringstart ||
87 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
88 				return (FNM_NOMATCH);
89 			++string;
90 			break;
91 		case '*':
92 			c = *pattern;
93 			/* Collapse multiple stars. */
94 			while (c == '*')
95 				c = *++pattern;
96 
97 			if (*string == '.' && (flags & FNM_PERIOD) &&
98 			    (string == stringstart ||
99 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
100 				return (FNM_NOMATCH);
101 
102 			/* Optimize for pattern with * at end or before /. */
103 			if (c == EOS) {
104 				if (flags & FNM_PATHNAME)
105 					return ((flags & FNM_LEADING_DIR) ||
106 					    strchr(string, '/') == NULL ?
107 					    0 : FNM_NOMATCH);
108 				else
109 					return (0);
110 			} else if (c == '/' && (flags & FNM_PATHNAME)) {
111 				if ((string = strchr(string, '/')) == NULL)
112 					return (FNM_NOMATCH);
113 				break;
114 			}
115 
116 			/* General case, use recursion. */
117 			while ((test = *string) != EOS) {
118 				if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
119 					return (0);
120 				if (test == '/' && (flags & FNM_PATHNAME))
121 					break;
122 				++string;
123 			}
124 			return (FNM_NOMATCH);
125 		case '[':
126 			if (*string == EOS)
127 				return (FNM_NOMATCH);
128 			if (*string == '/' && (flags & FNM_PATHNAME))
129 				return (FNM_NOMATCH);
130 			if (*string == '.' && (flags & FNM_PERIOD) &&
131 			    (string == stringstart ||
132 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
133 				return (FNM_NOMATCH);
134 
135 			switch (rangematch(pattern, *string, flags, &newp)) {
136 			case RANGE_ERROR:
137 				/* not a good range, treat as normal text */
138 				goto normal;
139 			case RANGE_MATCH:
140 				pattern = newp;
141 				break;
142 			case RANGE_NOMATCH:
143 				return (FNM_NOMATCH);
144 			}
145 			++string;
146 			break;
147 		case '\\':
148 			if (!(flags & FNM_NOESCAPE)) {
149 				if ((c = *pattern++) == EOS) {
150 					c = '\\';
151 					--pattern;
152 				}
153 			}
154 			/* FALLTHROUGH */
155 		default:
156 		normal:
157 			if (c != *string && !((flags & FNM_CASEFOLD) &&
158 				 (tolower((unsigned char)c) ==
159 				 tolower((unsigned char)*string))))
160 				return (FNM_NOMATCH);
161 			++string;
162 			break;
163 		}
164 	/* NOTREACHED */
165 }
166 
167 static int
rangematch(const char * pattern,char test,int flags,char ** newp)168 rangematch(const char *pattern, char test, int flags, char **newp)
169 {
170 	int negate, ok;
171 	char c, c2;
172 
173 	/*
174 	 * A bracket expression starting with an unquoted circumflex
175 	 * character produces unspecified results (IEEE 1003.2-1992,
176 	 * 3.13.2).  This implementation treats it like '!', for
177 	 * consistency with the regular expression syntax.
178 	 * J.T. Conklin (conklin@ngai.kaleida.com)
179 	 */
180 	if ((negate = (*pattern == '!' || *pattern == '^')))
181 		++pattern;
182 
183 	if (flags & FNM_CASEFOLD)
184 		test = tolower((unsigned char)test);
185 
186 	/*
187 	 * A right bracket shall lose its special meaning and represent
188 	 * itself in a bracket expression if it occurs first in the list.
189 	 * -- POSIX.2 2.8.3.2
190 	 */
191 	ok = 0;
192 	c = *pattern++;
193 	do {
194 		if (c == '\\' && !(flags & FNM_NOESCAPE))
195 			c = *pattern++;
196 		if (c == EOS)
197 			return (RANGE_ERROR);
198 		if (c == '/' && (flags & FNM_PATHNAME))
199 			return (RANGE_NOMATCH);
200 		if ((flags & FNM_CASEFOLD))
201 			c = tolower((unsigned char)c);
202 		if (*pattern == '-'
203 		    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
204 			pattern += 2;
205 			if (c2 == '\\' && !(flags & FNM_NOESCAPE))
206 				c2 = *pattern++;
207 			if (c2 == EOS)
208 				return (RANGE_ERROR);
209 			if (flags & FNM_CASEFOLD)
210 				c2 = tolower((unsigned char)c2);
211 			if (c <= test && test <= c2)
212 				ok = 1;
213 		} else if (c == test)
214 			ok = 1;
215 	} while ((c = *pattern++) != ']');
216 
217 	*newp = (char *)pattern;
218 	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
219 }
220