comparison glob/fnmatch.c @ 4096:66d7394f5822

[project @ 2002-10-09 18:47:16 by jwe]
author jwe
date Wed, 09 Oct 2002 18:47:16 +0000
parents 655b1615eb54
children
comparison
equal deleted inserted replaced
4095:60abc5f86565 4096:66d7394f5822
1 /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc. 1 /* Copyright (C) 1991, 92, 93, 96, 97, 98, 99 Free Software Foundation, Inc.
2 2 This file is part of the GNU C Library.
3 This library is free software; you can redistribute it and/or 3
4 modify it under the terms of the GNU Library General Public License as 4 This library is free software; you can redistribute it and/or
5 published by the Free Software Foundation; either version 2 of the 5 modify it under the terms of the GNU Library General Public License as
6 License, or (at your option) any later version. 6 published by the Free Software Foundation; either version 2 of the
7 7 License, or (at your option) any later version.
8 This library is distributed in the hope that it will be useful, 8
9 but WITHOUT ANY WARRANTY; without even the implied warranty of 9 This library is distributed in the hope that it will be useful,
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 Library General Public License for more details. 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 12 Library General Public License for more details.
13 You should have received a copy of the GNU Library General Public 13
14 License along with this library; see the file COPYING.LIB. If 14 You should have received a copy of the GNU Library General Public
15 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 15 License along with this library; see the file COPYING.LIB. If not,
16 Boston, MA 02111-1307, USA. */ 16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 17 Boston, MA 02111-1307, USA. */
18 #ifdef HAVE_CONFIG_H 18
19 #include <config.h> 19 #if HAVE_CONFIG_H
20 # include <config.h>
20 #endif 21 #endif
21 22
22 /* Enable GNU extensions in fnmatch.h. */ 23 /* Enable GNU extensions in fnmatch.h. */
23 #ifndef _GNU_SOURCE 24 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE 1 25 # define _GNU_SOURCE 1
25 #endif 26 #endif
26 27
27 #include <errno.h> 28 #include <errno.h>
28 #include <fnmatch.h> 29 #include <fnmatch.h>
29 #include <ctype.h> 30 #include <ctype.h>
30 31
32 #if HAVE_STRING_H || defined _LIBC
33 # include <string.h>
34 #else
35 # include <strings.h>
36 #endif
37
38 #if defined STDC_HEADERS || defined _LIBC
39 # include <stdlib.h>
40 #endif
41
42 /* For platform which support the ISO C amendement 1 functionality we
43 support user defined character classes. */
44 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
45 /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
46 # include <wchar.h>
47 # include <wctype.h>
48 #endif
31 49
32 /* Comment out all this code if we are using the GNU C Library, and are not 50 /* Comment out all this code if we are using the GNU C Library, and are not
33 actually compiling the library itself. This code is part of the GNU C 51 actually compiling the library itself. This code is part of the GNU C
34 Library, but also included in many other GNU distributions. Compiling 52 Library, but also included in many other GNU distributions. Compiling
35 and linking in this code is a waste when using the GNU C library 53 and linking in this code is a waste when using the GNU C library
36 (especially if it is a shared library). Rather than having every GNU 54 (especially if it is a shared library). Rather than having every GNU
37 program understand `configure --with-gnu-libc' and omit the object files, 55 program understand `configure --with-gnu-libc' and omit the object files,
38 it is simpler to just do this in the source for each such file. */ 56 it is simpler to just do this in the source for each such file. */
39 57
40 #if defined (_LIBC) || !defined (__GNU_LIBRARY__) 58 #if defined _LIBC || !defined __GNU_LIBRARY__
41 59
42 60
43 #ifndef errno 61 # if defined STDC_HEADERS || !defined isascii
62 # define ISASCII(c) 1
63 # else
64 # define ISASCII(c) isascii(c)
65 # endif
66
67 # ifdef isblank
68 # define ISBLANK(c) (ISASCII (c) && isblank (c))
69 # else
70 # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
71 # endif
72 # ifdef isgraph
73 # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
74 # else
75 # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
76 # endif
77
78 # define ISPRINT(c) (ISASCII (c) && isprint (c))
79 # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
80 # define ISALNUM(c) (ISASCII (c) && isalnum (c))
81 # define ISALPHA(c) (ISASCII (c) && isalpha (c))
82 # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
83 # define ISLOWER(c) (ISASCII (c) && islower (c))
84 # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
85 # define ISSPACE(c) (ISASCII (c) && isspace (c))
86 # define ISUPPER(c) (ISASCII (c) && isupper (c))
87 # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
88
89 # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
90
91 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
92 /* The GNU C library provides support for user-defined character classes
93 and the functions from ISO C amendement 1. */
94 # ifdef CHARCLASS_NAME_MAX
95 # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
96 # else
97 /* This shouldn't happen but some implementation might still have this
98 problem. Use a reasonable default value. */
99 # define CHAR_CLASS_MAX_LENGTH 256
100 # endif
101
102 # ifdef _LIBC
103 # define IS_CHAR_CLASS(string) __wctype (string)
104 # else
105 # define IS_CHAR_CLASS(string) wctype (string)
106 # endif
107 # else
108 # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
109
110 # define IS_CHAR_CLASS(string) \
111 (STREQ (string, "alpha") || STREQ (string, "upper") \
112 || STREQ (string, "lower") || STREQ (string, "digit") \
113 || STREQ (string, "alnum") || STREQ (string, "xdigit") \
114 || STREQ (string, "space") || STREQ (string, "print") \
115 || STREQ (string, "punct") || STREQ (string, "graph") \
116 || STREQ (string, "cntrl") || STREQ (string, "blank"))
117 # endif
118
119 /* Avoid depending on library functions or files
120 whose names are inconsistent. */
121
122 # if !defined _LIBC && !defined getenv
123 extern char *getenv ();
124 # endif
125
126 # ifndef errno
44 extern int errno; 127 extern int errno;
45 #endif 128 # endif
129
130 /* This function doesn't exist on most systems. */
131
132 # if !defined HAVE___STRCHRNUL && !defined _LIBC
133 static char *
134 __strchrnul (s, c)
135 const char *s;
136 int c;
137 {
138 char *result = strchr (s, c);
139 if (result == NULL)
140 result = strchr (s, '\0');
141 return result;
142 }
143 # endif
144
145 # ifndef internal_function
146 /* Inside GNU libc we mark some function in a special way. In other
147 environments simply ignore the marking. */
148 # define internal_function
149 # endif
46 150
47 /* Match STRING against the filename pattern PATTERN, returning zero if 151 /* Match STRING against the filename pattern PATTERN, returning zero if
48 it matches, nonzero if not. */ 152 it matches, nonzero if not. */
49 int 153 static int internal_fnmatch __P ((const char *pattern, const char *string,
50 fnmatch (pattern, string, flags) 154 int no_leading_period, int flags))
155 internal_function;
156 static int
157 internal_function
158 internal_fnmatch (pattern, string, no_leading_period, flags)
51 const char *pattern; 159 const char *pattern;
52 const char *string; 160 const char *string;
161 int no_leading_period;
53 int flags; 162 int flags;
54 { 163 {
55 register const char *p = pattern, *n = string; 164 register const char *p = pattern, *n = string;
56 register char c; 165 register unsigned char c;
57 166
58 /* Note that this evalutes C many times. */ 167 /* Note that this evaluates C many times. */
59 #define FOLD(c) ((flags & FNM_CASEFOLD) && isupper (c) ? tolower (c) : (c)) 168 # ifdef _LIBC
169 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
170 # else
171 # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
172 # endif
60 173
61 while ((c = *p++) != '\0') 174 while ((c = *p++) != '\0')
62 { 175 {
63 c = FOLD (c); 176 c = FOLD (c);
64 177
65 switch (c) 178 switch (c)
66 { 179 {
67 case '?': 180 case '?':
68 if (*n == '\0') 181 if (*n == '\0')
69 return FNM_NOMATCH; 182 return FNM_NOMATCH;
70 else if ((flags & FNM_FILE_NAME) && *n == '/') 183 else if (*n == '/' && (flags & FNM_FILE_NAME))
71 return FNM_NOMATCH; 184 return FNM_NOMATCH;
72 else if ((flags & FNM_PERIOD) && *n == '.' && 185 else if (*n == '.' && no_leading_period
73 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) 186 && (n == string
187 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
74 return FNM_NOMATCH; 188 return FNM_NOMATCH;
75 break; 189 break;
76 190
77 case '\\': 191 case '\\':
78 if (!(flags & FNM_NOESCAPE)) 192 if (!(flags & FNM_NOESCAPE))
81 if (c == '\0') 195 if (c == '\0')
82 /* Trailing \ loses. */ 196 /* Trailing \ loses. */
83 return FNM_NOMATCH; 197 return FNM_NOMATCH;
84 c = FOLD (c); 198 c = FOLD (c);
85 } 199 }
86 if (FOLD (*n) != c) 200 if (FOLD ((unsigned char) *n) != c)
87 return FNM_NOMATCH; 201 return FNM_NOMATCH;
88 break; 202 break;
89 203
90 case '*': 204 case '*':
91 if ((flags & FNM_PERIOD) && *n == '.' && 205 if (*n == '.' && no_leading_period
92 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) 206 && (n == string
207 || (n[-1] == '/' && (flags & FNM_FILE_NAME))))
93 return FNM_NOMATCH; 208 return FNM_NOMATCH;
94 209
95 for (c = *p++; c == '?' || c == '*'; c = *p++) 210 for (c = *p++; c == '?' || c == '*'; c = *p++)
96 { 211 {
97 if ((flags & FNM_FILE_NAME) && *n == '/') 212 if (*n == '/' && (flags & FNM_FILE_NAME))
98 /* A slash does not match a wildcard under FNM_FILE_NAME. */ 213 /* A slash does not match a wildcard under FNM_FILE_NAME. */
99 return FNM_NOMATCH; 214 return FNM_NOMATCH;
100 else if (c == '?') 215 else if (c == '?')
101 { 216 {
102 /* A ? needs to match one character. */ 217 /* A ? needs to match one character. */
110 ++n; 225 ++n;
111 } 226 }
112 } 227 }
113 228
114 if (c == '\0') 229 if (c == '\0')
115 return 0; 230 /* The wildcard(s) is/are the last element of the pattern.
116 231 If the name is a file name and contains another slash
117 { 232 this does mean it cannot match. */
118 char c1 = (!(flags & FNM_NOESCAPE) && c == '\\') ? *p : c; 233 return ((flags & FNM_FILE_NAME) && strchr (n, '/') != NULL
119 c1 = FOLD (c1); 234 ? FNM_NOMATCH : 0);
120 for (--p; *n != '\0'; ++n) 235 else
121 if ((c == '[' || FOLD (*n) == c1) && 236 {
122 fnmatch (p, n, flags & ~FNM_PERIOD) == 0) 237 const char *endp;
123 return 0; 238
124 return FNM_NOMATCH; 239 endp = __strchrnul (n, (flags & FNM_FILE_NAME) ? '/' : '\0');
125 } 240
241 if (c == '[')
242 {
243 int flags2 = ((flags & FNM_FILE_NAME)
244 ? flags : (flags & ~FNM_PERIOD));
245
246 for (--p; n < endp; ++n)
247 if (internal_fnmatch (p, n,
248 (no_leading_period
249 && (n == string
250 || (n[-1] == '/'
251 && (flags
252 & FNM_FILE_NAME)))),
253 flags2)
254 == 0)
255 return 0;
256 }
257 else if (c == '/' && (flags & FNM_FILE_NAME))
258 {
259 while (*n != '\0' && *n != '/')
260 ++n;
261 if (*n == '/'
262 && (internal_fnmatch (p, n + 1, flags & FNM_PERIOD,
263 flags) == 0))
264 return 0;
265 }
266 else
267 {
268 int flags2 = ((flags & FNM_FILE_NAME)
269 ? flags : (flags & ~FNM_PERIOD));
270
271 if (c == '\\' && !(flags & FNM_NOESCAPE))
272 c = *p;
273 c = FOLD (c);
274 for (--p; n < endp; ++n)
275 if (FOLD ((unsigned char) *n) == c
276 && (internal_fnmatch (p, n,
277 (no_leading_period
278 && (n == string
279 || (n[-1] == '/'
280 && (flags
281 & FNM_FILE_NAME)))),
282 flags2) == 0))
283 return 0;
284 }
285 }
286
287 /* If we come here no match is possible with the wildcard. */
288 return FNM_NOMATCH;
126 289
127 case '[': 290 case '[':
128 { 291 {
129 /* Nonzero if the sense of the character class is inverted. */ 292 /* Nonzero if the sense of the character class is inverted. */
293 static int posixly_correct;
130 register int not; 294 register int not;
295 char cold;
296
297 if (posixly_correct == 0)
298 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
131 299
132 if (*n == '\0') 300 if (*n == '\0')
133 return FNM_NOMATCH; 301 return FNM_NOMATCH;
134 302
135 if ((flags & FNM_PERIOD) && *n == '.' && 303 if (*n == '.' && no_leading_period && (n == string
136 (n == string || ((flags & FNM_FILE_NAME) && n[-1] == '/'))) 304 || (n[-1] == '/'
305 && (flags
306 & FNM_FILE_NAME))))
137 return FNM_NOMATCH; 307 return FNM_NOMATCH;
138 308
139 not = (*p == '!' || *p == '^'); 309 if (*n == '/' && (flags & FNM_FILE_NAME))
310 /* `/' cannot be matched. */
311 return FNM_NOMATCH;
312
313 not = (*p == '!' || (posixly_correct < 0 && *p == '^'));
140 if (not) 314 if (not)
141 ++p; 315 ++p;
142 316
143 c = *p++; 317 c = *p++;
144 for (;;) 318 for (;;)
145 { 319 {
146 register char cstart = c, cend = c; 320 unsigned char fn = FOLD ((unsigned char) *n);
147 321
148 if (!(flags & FNM_NOESCAPE) && c == '\\') 322 if (!(flags & FNM_NOESCAPE) && c == '\\')
149 { 323 {
150 if (*p == '\0') 324 if (*p == '\0')
151 return FNM_NOMATCH; 325 return FNM_NOMATCH;
152 cstart = cend = *p++; 326 c = FOLD ((unsigned char) *p);
327 ++p;
328
329 if (c == fn)
330 goto matched;
153 } 331 }
154 332 else if (c == '[' && *p == ':')
155 cstart = cend = FOLD (cstart); 333 {
156 334 /* Leave room for the null. */
157 if (c == '\0') 335 char str[CHAR_CLASS_MAX_LENGTH + 1];
336 size_t c1 = 0;
337 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
338 wctype_t wt;
339 # endif
340 const char *startp = p;
341
342 for (;;)
343 {
344 if (c1 == CHAR_CLASS_MAX_LENGTH)
345 /* The name is too long and therefore the pattern
346 is ill-formed. */
347 return FNM_NOMATCH;
348
349 c = *++p;
350 if (c == ':' && p[1] == ']')
351 {
352 p += 2;
353 break;
354 }
355 if (c < 'a' || c >= 'z')
356 {
357 /* This cannot possibly be a character class name.
358 Match it as a normal range. */
359 p = startp;
360 c = '[';
361 goto normal_bracket;
362 }
363 str[c1++] = c;
364 }
365 str[c1] = '\0';
366
367 # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
368 wt = IS_CHAR_CLASS (str);
369 if (wt == 0)
370 /* Invalid character class name. */
371 return FNM_NOMATCH;
372
373 if (__iswctype (__btowc ((unsigned char) *n), wt))
374 goto matched;
375 # else
376 if ((STREQ (str, "alnum") && ISALNUM ((unsigned char) *n))
377 || (STREQ (str, "alpha") && ISALPHA ((unsigned char) *n))
378 || (STREQ (str, "blank") && ISBLANK ((unsigned char) *n))
379 || (STREQ (str, "cntrl") && ISCNTRL ((unsigned char) *n))
380 || (STREQ (str, "digit") && ISDIGIT ((unsigned char) *n))
381 || (STREQ (str, "graph") && ISGRAPH ((unsigned char) *n))
382 || (STREQ (str, "lower") && ISLOWER ((unsigned char) *n))
383 || (STREQ (str, "print") && ISPRINT ((unsigned char) *n))
384 || (STREQ (str, "punct") && ISPUNCT ((unsigned char) *n))
385 || (STREQ (str, "space") && ISSPACE ((unsigned char) *n))
386 || (STREQ (str, "upper") && ISUPPER ((unsigned char) *n))
387 || (STREQ (str, "xdigit") && ISXDIGIT ((unsigned char) *n)))
388 goto matched;
389 # endif
390 }
391 else if (c == '\0')
158 /* [ (unterminated) loses. */ 392 /* [ (unterminated) loses. */
159 return FNM_NOMATCH; 393 return FNM_NOMATCH;
160 394 else
161 c = *p++;
162 c = FOLD (c);
163
164 if ((flags & FNM_FILE_NAME) && c == '/')
165 /* [/] can never match. */
166 return FNM_NOMATCH;
167
168 if (c == '-' && *p != ']')
169 { 395 {
170 cend = *p++; 396 normal_bracket:
171 if (!(flags & FNM_NOESCAPE) && cend == '\\') 397 if (FOLD (c) == fn)
172 cend = *p++; 398 goto matched;
173 if (cend == '\0') 399
174 return FNM_NOMATCH; 400 cold = c;
175 cend = FOLD (cend);
176
177 c = *p++; 401 c = *p++;
402
403 if (c == '-' && *p != ']')
404 {
405 /* It is a range. */
406 unsigned char cend = *p++;
407 if (!(flags & FNM_NOESCAPE) && cend == '\\')
408 cend = *p++;
409 if (cend == '\0')
410 return FNM_NOMATCH;
411
412 if (cold <= fn && fn <= FOLD (cend))
413 goto matched;
414
415 c = *p++;
416 }
178 } 417 }
179
180 if (FOLD (*n) >= cstart && FOLD (*n) <= cend)
181 goto matched;
182 418
183 if (c == ']') 419 if (c == ']')
184 break; 420 break;
185 } 421 }
422
186 if (!not) 423 if (!not)
187 return FNM_NOMATCH; 424 return FNM_NOMATCH;
188 break; 425 break;
189 426
190 matched:; 427 matched:
191 /* Skip the rest of the [...] that already matched. */ 428 /* Skip the rest of the [...] that already matched. */
192 while (c != ']') 429 while (c != ']')
193 { 430 {
194 if (c == '\0') 431 if (c == '\0')
195 /* [... (unterminated) loses. */ 432 /* [... (unterminated) loses. */
201 if (*p == '\0') 438 if (*p == '\0')
202 return FNM_NOMATCH; 439 return FNM_NOMATCH;
203 /* XXX 1003.2d11 is unclear if this is right. */ 440 /* XXX 1003.2d11 is unclear if this is right. */
204 ++p; 441 ++p;
205 } 442 }
443 else if (c == '[' && *p == ':')
444 {
445 do
446 if (*++p == '\0')
447 return FNM_NOMATCH;
448 while (*p != ':' || p[1] == ']');
449 p += 2;
450 c = *p;
451 }
206 } 452 }
207 if (not) 453 if (not)
208 return FNM_NOMATCH; 454 return FNM_NOMATCH;
209 } 455 }
210 break; 456 break;
211 457
212 default: 458 default:
213 if (c != FOLD (*n)) 459 if (c != FOLD ((unsigned char) *n))
214 return FNM_NOMATCH; 460 return FNM_NOMATCH;
215 } 461 }
216 462
217 ++n; 463 ++n;
218 } 464 }
223 if ((flags & FNM_LEADING_DIR) && *n == '/') 469 if ((flags & FNM_LEADING_DIR) && *n == '/')
224 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */ 470 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
225 return 0; 471 return 0;
226 472
227 return FNM_NOMATCH; 473 return FNM_NOMATCH;
474
475 # undef FOLD
228 } 476 }
229 477
478
479 int
480 fnmatch (pattern, string, flags)
481 const char *pattern;
482 const char *string;
483 int flags;
484 {
485 return internal_fnmatch (pattern, string, flags & FNM_PERIOD, flags);
486 }
487
230 #endif /* _LIBC or not __GNU_LIBRARY__. */ 488 #endif /* _LIBC or not __GNU_LIBRARY__. */