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