Update.
[glibc.git] / posix / fnmatch_loop.c
blobb7baa7be247d3262fc59794723c1e31f53daf052
1 /* Copyright (C) 1991-1993, 1996-1999, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 /* Match STRING against the filename pattern PATTERN, returning zero if
20 it matches, nonzero if not. */
21 static int FCT (const CHAR *pattern, const CHAR *string,
22 int no_leading_period, int flags) internal_function;
24 static int
25 internal_function
26 FCT (pattern, string, no_leading_period, flags)
27 const CHAR *pattern;
28 const CHAR *string;
29 int no_leading_period;
30 int flags;
32 register const CHAR *p = pattern, *n = string;
33 register UCHAR c;
34 #ifdef _LIBC
35 const UCHAR *collseq = (const UCHAR *)
36 _NL_CURRENT(LC_COLLATE, CONCAT(_NL_COLLATE_COLLSEQ,SUFFIX));
37 # ifdef WIDE_CHAR_VERSION
38 const wint_t *names = (const wint_t *)
39 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_NAMES);
40 size_t size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_HASH_SIZE);
41 size_t layers = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_HASH_LAYERS);
42 # endif
43 #endif
45 while ((c = *p++) != L('\0'))
47 c = FOLD (c);
49 switch (c)
51 case L('?'):
52 if (*n == L('\0'))
53 return FNM_NOMATCH;
54 else if (*n == L('/') && (flags & FNM_FILE_NAME))
55 return FNM_NOMATCH;
56 else if (*n == L('.') && no_leading_period
57 && (n == string
58 || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
59 return FNM_NOMATCH;
60 break;
62 case L('\\'):
63 if (!(flags & FNM_NOESCAPE))
65 c = *p++;
66 if (c == L('\0'))
67 /* Trailing \ loses. */
68 return FNM_NOMATCH;
69 c = FOLD (c);
71 if (FOLD ((UCHAR) *n) != c)
72 return FNM_NOMATCH;
73 break;
75 case L('*'):
76 if (*n == L('.') && no_leading_period
77 && (n == string
78 || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
79 return FNM_NOMATCH;
81 for (c = *p++; c == L('?') || c == L('*'); c = *p++)
83 if (*n == L('/') && (flags & FNM_FILE_NAME))
84 /* A slash does not match a wildcard under FNM_FILE_NAME. */
85 return FNM_NOMATCH;
86 else if (c == L('?'))
88 /* A ? needs to match one character. */
89 if (*n == L('\0'))
90 /* There isn't another character; no match. */
91 return FNM_NOMATCH;
92 else
93 /* One character of the string is consumed in matching
94 this ? wildcard, so *??? won't match if there are
95 less than three characters. */
96 ++n;
100 if (c == L('\0'))
101 /* The wildcard(s) is/are the last element of the pattern.
102 If the name is a file name and contains another slash
103 this does mean it cannot match. If the FNM_LEADING_DIR
104 flag is set and exactly one slash is following, we have
105 a match. */
107 int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
109 if (flags & FNM_FILE_NAME)
111 const CHAR *slashp = STRCHR (n, L('/'));
113 if (flags & FNM_LEADING_DIR)
115 if (slashp != NULL
116 && STRCHR (slashp + 1, L('/')) == NULL)
117 result = 0;
119 else
121 if (slashp == NULL)
122 result = 0;
126 return result;
128 else
130 const CHAR *endp;
132 endp = STRCHRNUL (n, (flags & FNM_FILE_NAME) ? L('/') : L('\0'));
134 if (c == L('['))
136 int flags2 = ((flags & FNM_FILE_NAME)
137 ? flags : (flags & ~FNM_PERIOD));
139 for (--p; n < endp; ++n)
140 if (FCT (p, n, (no_leading_period
141 && (n == string
142 || (n[-1] == L('/')
143 && (flags & FNM_FILE_NAME)))),
144 flags2) == 0)
145 return 0;
147 else if (c == L('/') && (flags & FNM_FILE_NAME))
149 while (*n != L('\0') && *n != L('/'))
150 ++n;
151 if (*n == L('/')
152 && (FCT (p, n + 1, flags & FNM_PERIOD, flags) == 0))
153 return 0;
155 else
157 int flags2 = ((flags & FNM_FILE_NAME)
158 ? flags : (flags & ~FNM_PERIOD));
160 if (c == L('\\') && !(flags & FNM_NOESCAPE))
161 c = *p;
162 c = FOLD (c);
163 for (--p; n < endp; ++n)
164 if (FOLD ((UCHAR) *n) == c
165 && (FCT (p, n, (no_leading_period
166 && (n == string
167 || (n[-1] == L('/')
168 && (flags & FNM_FILE_NAME)))),
169 flags2) == 0))
170 return 0;
174 /* If we come here no match is possible with the wildcard. */
175 return FNM_NOMATCH;
177 case L('['):
179 static int posixly_correct;
180 /* Nonzero if the sense of the character class is inverted. */
181 register int not;
182 CHAR cold;
184 if (posixly_correct == 0)
185 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
187 if (*n == L('\0'))
188 return FNM_NOMATCH;
190 if (*n == L('.') && no_leading_period
191 && (n == string
192 || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
193 return FNM_NOMATCH;
195 if (*n == L('/') && (flags & FNM_FILE_NAME))
196 /* `/' cannot be matched. */
197 return FNM_NOMATCH;
199 not = (*p == L('!') || (posixly_correct < 0 && *p == L('^')));
200 if (not)
201 ++p;
203 c = *p++;
204 for (;;)
206 UCHAR fn = FOLD ((UCHAR) *n);
208 if (!(flags & FNM_NOESCAPE) && c == L('\\'))
210 if (*p == L('\0'))
211 return FNM_NOMATCH;
212 c = FOLD ((UCHAR) *p);
213 ++p;
215 if (c == fn)
216 goto matched;
218 else if (c == L('[') && *p == L(':'))
220 /* Leave room for the null. */
221 CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
222 size_t c1 = 0;
223 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
224 wctype_t wt;
225 #endif
226 const CHAR *startp = p;
228 for (;;)
230 if (c1 == CHAR_CLASS_MAX_LENGTH)
231 /* The name is too long and therefore the pattern
232 is ill-formed. */
233 return FNM_NOMATCH;
235 c = *++p;
236 if (c == L(':') && p[1] == L(']'))
238 p += 2;
239 break;
241 if (c < L('a') || c >= L('z'))
243 /* This cannot possibly be a character class name.
244 Match it as a normal range. */
245 p = startp;
246 c = L('[');
247 goto normal_bracket;
249 str[c1++] = c;
251 str[c1] = L('\0');
253 #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
254 wt = IS_CHAR_CLASS (str);
255 if (wt == 0)
256 /* Invalid character class name. */
257 return FNM_NOMATCH;
259 /* The following code is glibc specific but does
260 there a good job in sppeding up the code since
261 we can avoid the btowc() call. The
262 IS_CHAR_CLASS call will return a bit mask for
263 the 32-bit table. We have to convert it to a
264 bitmask for the __ctype_b table. This has to
265 be done based on the byteorder as can be seen
266 below. In any case we will fall back on the
267 code using btowc() if the class is not one of
268 the standard classes. */
269 # if defined _LIBC && ! WIDE_CHAR_VERSION
270 # if __BYTE_ORDER == __LITTLE_ENDIAN
271 if ((wt & 0xf0ffff) == 0)
273 wt >>= 16;
274 if ((__ctype_b[(UCHAR) *n] & wt) != 0)
275 goto matched;
277 # else
278 if (wt <= 0x800)
280 if ((__ctype_b[(UCHAR) *n] & wt) != 0)
281 goto matched;
283 # endif
284 else
285 # endif
286 if (ISWCTYPE (BTOWC ((UCHAR) *n), wt))
287 goto matched;
288 #else
289 if ((STREQ (str, L("alnum")) && ISALNUM ((UCHAR) *n))
290 || (STREQ (str, L("alpha")) && ISALPHA ((UCHAR) *n))
291 || (STREQ (str, L("blank")) && ISBLANK ((UCHAR) *n))
292 || (STREQ (str, L("cntrl")) && ISCNTRL ((UCHAR) *n))
293 || (STREQ (str, L("digit")) && ISDIGIT ((UCHAR) *n))
294 || (STREQ (str, L("graph")) && ISGRAPH ((UCHAR) *n))
295 || (STREQ (str, L("lower")) && ISLOWER ((UCHAR) *n))
296 || (STREQ (str, L("print")) && ISPRINT ((UCHAR) *n))
297 || (STREQ (str, L("punct")) && ISPUNCT ((UCHAR) *n))
298 || (STREQ (str, L("space")) && ISSPACE ((UCHAR) *n))
299 || (STREQ (str, L("upper")) && ISUPPER ((UCHAR) *n))
300 || (STREQ (str, L("xdigit")) && ISXDIGIT ((UCHAR) *n)))
301 goto matched;
302 #endif
303 c = *p++;
305 #ifdef _LIBC
306 else if (c == L('[') && *p == L('='))
308 UCHAR str[1];
309 uint32_t nrules =
310 _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
311 const CHAR *startp = p;
313 c = *++p;
314 if (c == L('\0'))
316 p = startp;
317 c = L('[');
318 goto normal_bracket;
320 str[0] = c;
322 c = *++p;
323 if (c != L('=') || p[1] != L(']'))
325 p = startp;
326 c = L('[');
327 goto normal_bracket;
329 p += 2;
331 if (nrules == 0)
333 if ((UCHAR) *n == str[0])
334 goto matched;
336 else
338 const int32_t *table;
339 # if WIDE_CHAR_VERSION
340 const int32_t *weights;
341 const int32_t *extra;
342 # else
343 const unsigned char *weights;
344 const unsigned char *extra;
345 # endif
346 const int32_t *indirect;
347 int32_t idx;
348 const UCHAR *cp = (const UCHAR *) str;
350 /* This #include defines a local function! */
351 # if WIDE_CHAR_VERSION
352 # include <locale/weightwc.h>
353 # else
354 # include <locale/weight.h>
355 # endif
357 # if WIDE_CHAR_VERSION
358 table = (const int32_t *)
359 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
360 weights = (const int32_t *)
361 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
362 extra = (const int32_t *)
363 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
364 indirect = (const int32_t *)
365 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
366 # else
367 table = (const int32_t *)
368 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
369 weights = (const unsigned char *)
370 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
371 extra = (const unsigned char *)
372 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
373 indirect = (const int32_t *)
374 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
375 # endif
377 idx = findidx (&cp);
378 if (idx != 0)
380 /* We found a table entry. Now see whether the
381 character we are currently at has the same
382 equivalance class value. */
383 # if !WIDE_CHAR_VERSION
384 int len = weights[idx];
385 # endif
386 int32_t idx2;
387 const UCHAR *np = (const UCHAR *) n;
389 idx2 = findidx (&np);
390 # if WIDE_CHAR_VERSION
391 if (idx2 != 0 && weights[idx] == weights[idx2])
392 goto matched;
393 # else
394 if (idx2 != 0 && len == weights[idx2])
396 int cnt = 0;
398 while (cnt < len
399 && (weights[idx + 1 + cnt]
400 == weights[idx2 + 1 + cnt]))
401 ++cnt;
403 if (cnt == len)
404 goto matched;
406 # endif
410 c = *p++;
412 #endif
413 else if (c == L('\0'))
414 /* [ (unterminated) loses. */
415 return FNM_NOMATCH;
416 else
418 int is_seqval = 0;
419 int is_range = 0;
421 #ifdef _LIBC
422 if (c == L('[') && *p == L('.'))
424 uint32_t nrules =
425 _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
426 const CHAR *startp = p;
427 size_t c1 = 0;
429 while (1)
431 c = *++p;
432 if (c == L('.') && p[1] == L(']'))
434 p += 2;
435 break;
437 if (c == '\0')
438 return FNM_NOMATCH;
439 ++c1;
442 /* We have to handling the symbols differently in
443 ranges since then the collation sequence is
444 important. */
445 is_range = *p == L('-') && p[1] != L('\0');
447 if (nrules == 0)
449 /* There are no names defined in the collation
450 data. Therefore we only accept the trivial
451 names consisting of the character itself. */
452 if (c1 != 1)
453 return FNM_NOMATCH;
455 if (!is_range && *n == startp[1])
456 goto matched;
458 cold = startp[1];
459 c = *p++;
461 else
463 int32_t table_size;
464 const int32_t *symb_table;
465 # ifdef WIDE_CHAR_VERSION
466 char str[c1];
467 int strcnt;
468 # else
469 # define str (startp + 1)
470 # endif
471 const unsigned char *extra;
472 int32_t idx;
473 int32_t elem;
474 int32_t second;
475 int32_t hash;
477 # ifdef WIDE_CHAR_VERSION
478 /* We have to convert the name to a single-byte
479 string. This is possible since the names
480 consist of ASCII characters and the internal
481 representation is UCS4. */
482 for (strcnt = 0; strcnt < c1; ++strcnt)
483 str[strcnt] = startp[1 + strcnt];
484 #endif
486 table_size =
487 _NL_CURRENT_WORD (LC_COLLATE,
488 _NL_COLLATE_SYMB_HASH_SIZEMB);
489 symb_table = (const int32_t *)
490 _NL_CURRENT (LC_COLLATE,
491 _NL_COLLATE_SYMB_TABLEMB);
492 extra = (const unsigned char *)
493 _NL_CURRENT (LC_COLLATE,
494 _NL_COLLATE_SYMB_EXTRAMB);
496 /* Locate the character in the hashing table. */
497 hash = elem_hash (str, c1);
499 idx = 0;
500 elem = hash % table_size;
501 second = hash % (table_size - 2);
502 while (symb_table[2 * elem] != 0)
504 /* First compare the hashing value. */
505 if (symb_table[2 * elem] == hash
506 && c1 == extra[symb_table[2 * elem + 1]]
507 && memcmp (str,
508 &extra[symb_table[2 * elem + 1]
509 + 1], c1) == 0)
511 /* Yep, this is the entry. */
512 idx = symb_table[2 * elem + 1];
513 idx += 1 + extra[idx];
514 break;
517 /* Next entry. */
518 elem += second;
521 if (symb_table[2 * elem] != 0)
523 /* Compare the byte sequence but only if
524 this is not part of a range. */
525 # ifdef WIDE_CHAR_VERSION
526 int32_t *wextra;
528 idx += 1 + extra[idx];
529 /* Adjust for the alignment. */
530 idx = (idx + 3) & ~4;
532 wextra = (int32_t *) &extra[idx + 4];
533 # endif
535 if (! is_range)
537 # ifdef WIDE_CHAR_VERSION
538 for (c1 = 0; c1 < wextra[idx]; ++c1)
539 if (n[c1] != wextra[1 + c1])
540 break;
542 if (c1 == wextra[idx])
543 goto matched;
544 # else
545 for (c1 = 0; c1 < extra[idx]; ++c1)
546 if (n[c1] != extra[1 + c1])
547 break;
549 if (c1 == extra[idx])
550 goto matched;
551 # endif
554 /* Get the collation sequence value. */
555 is_seqval = 1;
556 # ifdef WIDE_CHAR_VERSION
557 cold = wextra[1 + wextra[idx]];
558 # else
559 /* Adjust for the alignment. */
560 idx += 1 + extra[idx];
561 idx = (idx + 3) & ~4;
562 cold = *((int32_t *) &extra[idx]);
563 # endif
565 c = *p++;
567 else if (symb_table[2 * elem] != 0 && c1 == 1)
569 /* No valid character. Match it as a
570 single byte. */
571 if (!is_range && *n == str[0])
572 goto matched;
574 cold = str[0];
575 c = *p++;
577 else
578 return FNM_NOMATCH;
581 else
582 # undef str
583 #endif
585 c = FOLD (c);
586 normal_bracket:
588 /* We have to handling the symbols differently in
589 ranges since then the collation sequence is
590 important. */
591 is_range = *p == L('-') && p[1] != L('\0');
593 if (!is_range && c == fn)
594 goto matched;
596 cold = c;
597 c = *p++;
600 if (c == L('-') && *p != L(']'))
602 #if _LIBC
603 /* We have to find the collation sequence
604 value for C. Collation sequence is nothing
605 we can regularly access. The sequence
606 value is defined by the order in which the
607 definitions of the collation values for the
608 various characters appear in the source
609 file. A strange concept, nowhere
610 documented. */
611 uint32_t fcollseq;
612 uint32_t lcollseq;
613 UCHAR cend = *p++;
614 # ifdef WIDE_CHAR_VERSION
615 int idx;
616 size_t cnt;
617 # endif
619 # ifdef WIDE_CHAR_VERSION
620 /* Search in the `names' array for the characters. */
621 idx = fn % size;
622 cnt = 0;
623 while (names[idx] != fn)
625 if (++cnt == layers)
626 /* XXX We don't know anything about
627 the character we are supposed to
628 match. This means we are failing. */
629 goto range_not_matched;
631 idx += size;
633 fcollseq = collseq[idx];
635 if (is_seqval)
636 lcollseq = cold;
637 else
639 idx = cold % size;
640 cnt = 0;
641 while (names[idx] != cold)
643 if (++cnt == layers)
645 idx = -1;
646 break;
648 idx += size;
651 lcollseq = idx == -1 ? 0xffffffff : collseq[idx];
653 # else
654 fcollseq = collseq[fn];
655 lcollseq = is_seqval ? cold : collseq[(UCHAR) cold];
656 # endif
658 is_seqval = 0;
659 if (cend == L('[') && *p == L('.'))
661 uint32_t nrules =
662 _NL_CURRENT_WORD (LC_COLLATE,
663 _NL_COLLATE_NRULES);
664 const CHAR *startp = p;
665 size_t c1 = 0;
667 while (1)
669 c = *++p;
670 if (c == L('.') && p[1] == L(']'))
672 p += 2;
673 break;
675 if (c == '\0')
676 return FNM_NOMATCH;
677 ++c1;
680 if (nrules == 0)
682 /* There are no names defined in the
683 collation data. Therefore we only
684 accept the trivial names consisting
685 of the character itself. */
686 if (c1 != 1)
687 return FNM_NOMATCH;
689 cend = startp[1];
691 else
693 int32_t table_size;
694 const int32_t *symb_table;
695 # ifdef WIDE_CHAR_VERSION
696 char str[c1];
697 int strcnt;
698 # else
699 # define str (startp + 1)
700 # endif
701 const unsigned char *extra;
702 int32_t idx;
703 int32_t elem;
704 int32_t second;
705 int32_t hash;
707 # ifdef WIDE_CHAR_VERSION
708 /* We have to convert the name to a single-byte
709 string. This is possible since the names
710 consist of ASCII characters and the internal
711 representation is UCS4. */
712 for (strcnt = 0; strcnt < c1; ++strcnt)
713 str[strcnt] = startp[1 + strcnt];
714 #endif
716 table_size =
717 _NL_CURRENT_WORD (LC_COLLATE,
718 _NL_COLLATE_SYMB_HASH_SIZEMB);
719 symb_table = (const int32_t *)
720 _NL_CURRENT (LC_COLLATE,
721 _NL_COLLATE_SYMB_TABLEMB);
722 extra = (const unsigned char *)
723 _NL_CURRENT (LC_COLLATE,
724 _NL_COLLATE_SYMB_EXTRAMB);
726 /* Locate the character in the hashing
727 table. */
728 hash = elem_hash (str, c1);
730 idx = 0;
731 elem = hash % table_size;
732 second = hash % (table_size - 2);
733 while (symb_table[2 * elem] != 0)
735 /* First compare the hashing value. */
736 if (symb_table[2 * elem] == hash
737 && (c1
738 == extra[symb_table[2 * elem + 1]])
739 && memcmp (str,
740 &extra[symb_table[2 * elem + 1]
741 + 1], c1) == 0)
743 /* Yep, this is the entry. */
744 idx = symb_table[2 * elem + 1];
745 idx += 1 + extra[idx];
746 break;
749 /* Next entry. */
750 elem += second;
753 if (symb_table[2 * elem] != 0)
755 /* Compare the byte sequence but only if
756 this is not part of a range. */
757 # ifdef WIDE_CHAR_VERSION
758 int32_t *wextra;
760 idx += 1 + extra[idx];
761 /* Adjust for the alignment. */
762 idx = (idx + 3) & ~4;
764 wextra = (int32_t *) &extra[idx + 4];
765 # endif
766 /* Get the collation sequence value. */
767 is_seqval = 1;
768 # ifdef WIDE_CHAR_VERSION
769 cend = wextra[1 + wextra[idx]];
770 # else
771 /* Adjust for the alignment. */
772 idx += 1 + extra[idx];
773 idx = (idx + 3) & ~4;
774 cend = *((int32_t *) &extra[idx]);
775 # endif
777 else if (symb_table[2 * elem] != 0 && c1 == 1)
779 cend = str[0];
780 c = *p++;
782 else
783 return FNM_NOMATCH;
785 # undef str
787 else
789 if (!(flags & FNM_NOESCAPE) && cend == L('\\'))
790 cend = *p++;
791 if (cend == L('\0'))
792 return FNM_NOMATCH;
793 cend = FOLD (cend);
796 /* XXX It is not entirely clear to me how to handle
797 characters which are not mentioned in the
798 collation specification. */
799 if (
800 # ifdef WIDE_CHAR_VERSION
801 lcollseq == 0xffffffff ||
802 # endif
803 lcollseq <= fcollseq)
805 /* We have to look at the upper bound. */
806 uint32_t hcollseq;
808 if (is_seqval)
809 hcollseq = cend;
810 else
812 # ifdef WIDE_CHAR_VERSION
813 idx = cend % size;
814 cnt = 0;
815 while (names[idx] != cend)
817 if (++cnt == layers)
819 /* Hum, no information about the upper
820 bound. The matching succeeds if the
821 lower bound is matched exactly. */
822 if (idx == -1 && lcollseq != fcollseq)
823 goto range_not_matched;
825 goto matched;
828 hcollseq = collseq[idx];
829 # else
830 hcollseq = collseq[cend];
831 # endif
834 if (lcollseq <= hcollseq && fcollseq <= hcollseq)
835 goto matched;
837 # ifdef WIDE_CHAR_VERSION
838 range_not_matched:
839 # endif
840 #else
841 /* We use a boring value comparison of the character
842 values. This is better than comparing using
843 `strcoll' since the latter would have surprising
844 and sometimes fatal consequences. */
845 UCHAR cend = *p++;
847 if (!(flags & FNM_NOESCAPE) && cend == L('\\'))
848 cend = *p++;
849 if (cend == L('\0'))
850 return FNM_NOMATCH;
852 /* It is a range. */
853 if (cold <= fc && fc <= c)
854 goto matched;
855 #endif
857 c = *p++;
861 if (c == L(']'))
862 break;
865 if (!not)
866 return FNM_NOMATCH;
867 break;
869 matched:
870 /* Skip the rest of the [...] that already matched. */
873 ignore_next:
874 c = *p++;
876 if (c == L('\0'))
877 /* [... (unterminated) loses. */
878 return FNM_NOMATCH;
880 if (!(flags & FNM_NOESCAPE) && c == L('\\'))
882 if (*p == L('\0'))
883 return FNM_NOMATCH;
884 /* XXX 1003.2d11 is unclear if this is right. */
885 ++p;
887 else if (c == L('[') && *p == L(':'))
889 int c1 = 0;
890 const CHAR *startp = p;
892 while (1)
894 c = *++p;
895 if (++c1 == CHAR_CLASS_MAX_LENGTH)
896 return FNM_NOMATCH;
898 if (*p == L(':') && p[1] == L(']'))
899 break;
901 if (c < L('a') || c >= L('z'))
903 p = startp;
904 goto ignore_next;
907 p += 2;
908 c = *p++;
910 else if (c == L('[') && *p == L('='))
912 c = *++p;
913 if (c == L('\0'))
914 return FNM_NOMATCH;
915 c = *++p;
916 if (c != L('=') || p[1] != L(']'))
917 return FNM_NOMATCH;
918 p += 2;
919 c = *p++;
921 else if (c == L('[') && *p == L('.'))
923 ++p;
924 while (1)
926 c = *++p;
927 if (c == '\0')
928 return FNM_NOMATCH;
930 if (*p == L('.') && p[1] == L(']'))
931 break;
933 p += 2;
934 c = *p++;
937 while (c != L(']'));
938 if (not)
939 return FNM_NOMATCH;
941 break;
943 default:
944 if (c != FOLD ((UCHAR) *n))
945 return FNM_NOMATCH;
948 ++n;
951 if (*n == '\0')
952 return 0;
954 if ((flags & FNM_LEADING_DIR) && *n == L('/'))
955 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
956 return 0;
958 return FNM_NOMATCH;
961 #undef FOLD
962 #undef CHAR
963 #undef UCHAR
964 #undef FCT
965 #undef STRCHR
966 #undef STRCHRNUL
967 #undef STRCOLL
968 #undef L
969 #undef BTOWC
970 #undef SUFFIX