]> Dogcows Code - chaz/tar/blob - lib/fnmatch.c
(fnmatch): Fix some FNM_FILE_NAME and FNM_LEADING_DIR bugs,
[chaz/tar] / lib / fnmatch.c
1 /* Copyright 1991, 1992, 1993, 1996, 1997, 2000 Free Software Foundation, Inc.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17 #if HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 /* Enable GNU extensions in fnmatch.h. */
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE 1
24 #endif
25
26 #include <errno.h>
27 #include <fnmatch.h>
28 #include <ctype.h>
29
30
31 /* Comment out all this code if we are using the GNU C Library, and are not
32 actually compiling the library itself. This code is part of the GNU C
33 Library, but also included in many other GNU distributions. Compiling
34 and linking in this code is a waste when using the GNU C library
35 (especially if it is a shared library). Rather than having every GNU
36 program understand `configure --with-gnu-libc' and omit the object files,
37 it is simpler to just do this in the source for each such file. */
38
39 #if defined _LIBC || !defined __GNU_LIBRARY__
40
41
42 # if defined STDC_HEADERS || !defined isascii
43 # define IN_CTYPE_DOMAIN(c) 1
44 # else
45 # define IN_CTYPE_DOMAIN(c) isascii(c)
46 # endif
47
48 # define ISUPPER(c) (IN_CTYPE_DOMAIN (c) && isupper (c))
49
50
51 # ifndef errno
52 extern int errno;
53 # endif
54
55 /* Match STRING against the filename pattern PATTERN, returning zero if
56 it matches, nonzero if not. */
57 int
58 fnmatch (const char *pattern, const char *string, int flags)
59 {
60 register const char *p = pattern, *n = string;
61 register char c;
62
63 /* Note that this evaluates C many times. */
64 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
65
66 while ((c = *p++) != '\0')
67 {
68 c = FOLD (c);
69
70 switch (c)
71 {
72 case '?':
73 if (*n == '\0')
74 return FNM_NOMATCH;
75 else if ((flags & FNM_FILE_NAME) && *n == '/')
76 return FNM_NOMATCH;
77 else if ((flags & FNM_PERIOD) && *n == '.' &&
78 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
79 return FNM_NOMATCH;
80 break;
81
82 case '\\':
83 if (!(flags & FNM_NOESCAPE))
84 {
85 c = *p++;
86 if (c == '\0')
87 /* Trailing \ loses. */
88 return FNM_NOMATCH;
89 c = FOLD (c);
90 }
91 if (FOLD (*n) != c)
92 return FNM_NOMATCH;
93 break;
94
95 case '*':
96 if ((flags & FNM_PERIOD) && *n == '.' &&
97 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
98 return FNM_NOMATCH;
99
100 for (c = *p++; c == '?' || c == '*'; c = *p++)
101 {
102 if (c == '?')
103 {
104 /* A ? needs to match one character. */
105 if (*n == '\0' || (*n == '/' && (flags & FNM_FILE_NAME)))
106 /* There isn't another character; no match. */
107 return FNM_NOMATCH;
108 else
109 /* One character of the string is consumed in matching
110 this ? wildcard, so *??? won't match if there are
111 less than three characters. */
112 ++n;
113 }
114 }
115
116 if (c == '\0')
117 {
118 if ((flags & (FNM_FILE_NAME | FNM_LEADING_DIR)) == FNM_FILE_NAME)
119 for (; *n != '\0'; n++)
120 if (*n == '/')
121 return FNM_NOMATCH;
122 return 0;
123 }
124
125 {
126 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c;
127 c1 = FOLD (c1);
128 for (--p; *n != '\0'; ++n)
129 if ((c == '[' || FOLD (*n) == c1) &&
130 fnmatch (p, n, flags & ~FNM_PERIOD) == 0)
131 return 0;
132 else if (*n == '/' && (flags & FNM_FILE_NAME))
133 break;
134 return FNM_NOMATCH;
135 }
136
137 case '[':
138 {
139 /* Nonzero if the sense of the character class is inverted. */
140 register int not;
141
142 if (*n == '\0')
143 return FNM_NOMATCH;
144
145 if ((flags & FNM_PERIOD) && *n == '.' &&
146 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/')))
147 return FNM_NOMATCH;
148
149 not = (*p == '!' || *p == '^');
150 if (not)
151 ++p;
152
153 c = *p++;
154 for (;;)
155 {
156 register char cstart = c, cend = c;
157
158 if (!(flags & FNM_NOESCAPE) && c == '\\')
159 {
160 if (*p == '\0')
161 return FNM_NOMATCH;
162 cstart = cend = *p++;
163 }
164
165 cstart = cend = FOLD (cstart);
166
167 if (c == '\0')
168 /* [ (unterminated) loses. */
169 return FNM_NOMATCH;
170
171 c = *p++;
172 c = FOLD (c);
173
174 if ((flags & FNM_FILE_NAME) && c == '/')
175 /* [/] can never match. */
176 return FNM_NOMATCH;
177
178 if (c == '-' && *p != ']')
179 {
180 cend = *p++;
181 if (!(flags & FNM_NOESCAPE) && cend == '\\')
182 cend = *p++;
183 if (cend == '\0')
184 return FNM_NOMATCH;
185 cend = FOLD (cend);
186
187 c = *p++;
188 }
189
190 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
191 goto matched;
192
193 if (c == ']')
194 break;
195 }
196 if (!not)
197 return FNM_NOMATCH;
198 break;
199
200 matched:;
201 /* Skip the rest of the [...] that already matched. */
202 while (c != ']')
203 {
204 if (c == '\0')
205 /* [... (unterminated) loses. */
206 return FNM_NOMATCH;
207
208 c = *p++;
209 if (!(flags & FNM_NOESCAPE) && c == '\\')
210 {
211 if (*p == '\0')
212 return FNM_NOMATCH;
213 /* XXX 1003.2d11 is unclear if this is right. */
214 ++p;
215 }
216 }
217 if (not)
218 return FNM_NOMATCH;
219 }
220 break;
221
222 default:
223 if (c != FOLD (*n))
224 return FNM_NOMATCH;
225 }
226
227 ++n;
228 }
229
230 if (*n == '\0')
231 return 0;
232
233 if ((flags & FNM_LEADING_DIR) && *n == '/')
234 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
235 return 0;
236
237 return FNM_NOMATCH;
238
239 # undef FOLD
240 }
241
242 #endif /* _LIBC or not __GNU_LIBRARY__. */
This page took 0.051302 seconds and 5 git commands to generate.