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  * 4. 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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 /*
34  * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
35  * Compares a filename or pathname to a pattern.
36  */
37 
38 #define _GNU_SOURCE
39 #include <ctype.h>
40 #include <fnmatch.h>
41 #include <string.h>
42 #include <stdio.h>
43 
44 #ifdef __HAVE_LOCALE_INFO__
45 #include "collate.h"
46 #endif
47 
48 #define	EOS	'\0'
49 
50 #define RANGE_MATCH     1
51 #define RANGE_NOMATCH   0
52 #define RANGE_ERROR     (-1)
53 
54 static int rangematch(const char *, char, int, char **);
55 
56 int
fnmatch(const char * pattern,const char * string,int flags)57 fnmatch(const char *pattern, const char *string, int flags)
58 {
59 	const char *stringstart;
60 	char *newp;
61 	char c, test;
62 
63 	for (stringstart = string;;)
64 		switch (c = *pattern++) {
65 		case EOS:
66 			if ((flags & FNM_LEADING_DIR) && *string == '/')
67 				return (0);
68 			return (*string == EOS ? 0 : FNM_NOMATCH);
69 		case '?':
70 			if (*string == EOS)
71 				return (FNM_NOMATCH);
72 			if (*string == '/' && (flags & FNM_PATHNAME))
73 				return (FNM_NOMATCH);
74 			if (*string == '.' && (flags & FNM_PERIOD) &&
75 			    (string == stringstart ||
76 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
77 				return (FNM_NOMATCH);
78 			++string;
79 			break;
80 		case '*':
81 			c = *pattern;
82 			/* Collapse multiple stars. */
83 			while (c == '*')
84 				c = *++pattern;
85 
86 			if (*string == '.' && (flags & FNM_PERIOD) &&
87 			    (string == stringstart ||
88 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
89 				return (FNM_NOMATCH);
90 
91 			/* Optimize for pattern with * at end or before /. */
92 			if (c == EOS)
93 				if (flags & FNM_PATHNAME)
94 					return ((flags & FNM_LEADING_DIR) ||
95 					    strchr(string, '/') == NULL ?
96 					    0 : FNM_NOMATCH);
97 				else
98 					return (0);
99 			else if (c == '/' && flags & FNM_PATHNAME) {
100 				if ((string = strchr(string, '/')) == NULL)
101 					return (FNM_NOMATCH);
102 				break;
103 			}
104 
105 			/* General case, use recursion. */
106 			while ((test = *string) != EOS) {
107 				if (!fnmatch(pattern, string, flags & ~FNM_PERIOD))
108 					return (0);
109 				if (test == '/' && flags & FNM_PATHNAME)
110 					break;
111 				++string;
112 			}
113 			return (FNM_NOMATCH);
114 		case '[':
115 			if (*string == EOS)
116 				return (FNM_NOMATCH);
117 			if (*string == '/' && (flags & FNM_PATHNAME))
118 				return (FNM_NOMATCH);
119 			if (*string == '.' && (flags & FNM_PERIOD) &&
120 			    (string == stringstart ||
121 			    ((flags & FNM_PATHNAME) && *(string - 1) == '/')))
122 				return (FNM_NOMATCH);
123 
124 			switch (rangematch(pattern, *string, flags, &newp)) {
125 			case RANGE_ERROR:
126 				goto norm;
127 			case RANGE_MATCH:
128 				pattern = newp;
129 				break;
130 			case RANGE_NOMATCH:
131 				return (FNM_NOMATCH);
132 			}
133 			++string;
134 			break;
135 		case '\\':
136 			if (!(flags & FNM_NOESCAPE)) {
137 				if ((c = *pattern++) == EOS) {
138 					c = '\\';
139 					--pattern;
140 				}
141 			}
142 			/* FALLTHROUGH */
143 		default:
144 		norm:
145 			if (c == *string)
146 				;
147 			else if ((flags & FNM_CASEFOLD) &&
148 				 (tolower((unsigned char)c) ==
149 				  tolower((unsigned char)*string)))
150 				;
151 			else
152 				return (FNM_NOMATCH);
153 			string++;
154 			break;
155 		}
156 	/* NOTREACHED */
157 }
158 
159 static int
rangematch(const char * pattern,char test,int flags,char ** newp)160 rangematch(const char *pattern, char test, int flags, char **newp)
161 {
162 	int negate, ok;
163 	char c, c2;
164 
165 	/*
166 	 * A bracket expression starting with an unquoted circumflex
167 	 * character produces unspecified results (IEEE 1003.2-1992,
168 	 * 3.13.2).  This implementation treats it like '!', for
169 	 * consistency with the regular expression syntax.
170 	 * J.T. Conklin (conklin@ngai.kaleida.com)
171 	 */
172 	if ( (negate = (*pattern == '!' || *pattern == '^')) )
173 		++pattern;
174 
175 	if (flags & FNM_CASEFOLD)
176 		test = tolower((unsigned char)test);
177 
178 	/*
179 	 * A right bracket shall lose its special meaning and represent
180 	 * itself in a bracket expression if it occurs first in the list.
181 	 * -- POSIX.2 2.8.3.2
182 	 */
183 	ok = 0;
184 	c = *pattern++;
185 	do {
186 		if (c == '\\' && !(flags & FNM_NOESCAPE))
187 			c = *pattern++;
188 		if (c == EOS)
189 			return (RANGE_ERROR);
190 
191 		if (c == '/' && (flags & FNM_PATHNAME))
192 			return (RANGE_NOMATCH);
193 
194 		if (flags & FNM_CASEFOLD)
195 			c = tolower((unsigned char)c);
196 
197 		if (*pattern == '-'
198 		    && (c2 = *(pattern+1)) != EOS && c2 != ']') {
199 			pattern += 2;
200 			if (c2 == '\\' && !(flags & FNM_NOESCAPE))
201 				c2 = *pattern++;
202 			if (c2 == EOS)
203 				return (RANGE_ERROR);
204 
205 			if (flags & FNM_CASEFOLD)
206 				c2 = tolower((unsigned char)c2);
207 
208 			if (
209 #ifdef __HAVE_LOCALE_INFO__
210                             __collate_load_error ?
211 			    c <= test && test <= c2 :
212 			       __collate_range_cmp(c, test) <= 0
213 			    && __collate_range_cmp(test, c2) <= 0
214 #else
215                             c <= test && test <= c2
216 #endif
217                             )
218 				ok = 1;
219 		} else if (c == test)
220 			ok = 1;
221 	} while ((c = *pattern++) != ']');
222 
223 	*newp = (char *)pattern;
224 	return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
225 }
226