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