1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 1991, 1992, 1993, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 2003, 2004, 2005, 2006, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of the GNU C Library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /* Match STRING against the file name pattern PATTERN, returning zero if
22 it matches, nonzero if not. */
23 static int EXT (INT opt, const CHAR *pattern, const CHAR *string,
24 const CHAR *string_end, bool no_leading_period, int flags)
26 static const CHAR *END (const CHAR *patternp) internal_function;
30 FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end,
31 bool no_leading_period, int flags)
33 register const CHAR *p = pattern, *n = string;
36 # if WIDE_CHAR_VERSION
37 const char *collseq = (const char *)
38 _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC);
40 const UCHAR *collseq = (const UCHAR *)
41 _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQMB);
45 while ((c = *p++) != L_('\0'))
47 bool new_no_leading_period = false;
53 if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
57 res = EXT (c, p, n, string_end, no_leading_period,
65 else if (*n == L_('/') && (flags & FNM_FILE_NAME))
67 else if (*n == L_('.') && no_leading_period)
72 if (!(flags & FNM_NOESCAPE))
76 /* Trailing \ loses. */
80 if (n == string_end || FOLD ((UCHAR) *n) != c)
85 if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
89 res = EXT (c, p, n, string_end, no_leading_period,
95 if (n != string_end && *n == L_('.') && no_leading_period)
98 for (c = *p++; c == L_('?') || c == L_('*'); c = *p++)
100 if (*p == L_('(') && (flags & FNM_EXTMATCH) != 0)
102 const CHAR *endp = END (p);
105 /* This is a pattern. Skip over it. */
113 /* A ? needs to match one character. */
115 /* There isn't another character; no match. */
117 else if (*n == L_('/')
118 && __builtin_expect (flags & FNM_FILE_NAME, 0))
119 /* A slash does not match a wildcard under
123 /* One character of the string is consumed in matching
124 this ? wildcard, so *??? won't match if there are
125 less than three characters. */
131 /* The wildcard(s) is/are the last element of the pattern.
132 If the name is a file name and contains another slash
133 this means it cannot match, unless the FNM_LEADING_DIR
136 int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
138 if (flags & FNM_FILE_NAME)
140 if (flags & FNM_LEADING_DIR)
144 if (MEMCHR (n, L_('/'), string_end - n) == NULL)
155 endp = MEMCHR (n, (flags & FNM_FILE_NAME) ? L_('/') : L_('\0'),
161 || (__builtin_expect (flags & FNM_EXTMATCH, 0) != 0
162 && (c == L_('@') || c == L_('+') || c == L_('!'))
165 int flags2 = ((flags & FNM_FILE_NAME)
166 ? flags : (flags & ~FNM_PERIOD));
167 bool no_leading_period2 = no_leading_period;
169 for (--p; n < endp; ++n, no_leading_period2 = false)
170 if (FCT (p, n, string_end, no_leading_period2, flags2)
174 else if (c == L_('/') && (flags & FNM_FILE_NAME))
176 while (n < string_end && *n != L_('/'))
178 if (n < string_end && *n == L_('/')
179 && (FCT (p, n + 1, string_end, flags & FNM_PERIOD, flags)
185 int flags2 = ((flags & FNM_FILE_NAME)
186 ? flags : (flags & ~FNM_PERIOD));
187 int no_leading_period2 = no_leading_period;
189 if (c == L_('\\') && !(flags & FNM_NOESCAPE))
192 for (--p; n < endp; ++n, no_leading_period2 = false)
193 if (FOLD ((UCHAR) *n) == c
194 && (FCT (p, n, string_end, no_leading_period2, flags2)
200 /* If we come here no match is possible with the wildcard. */
205 /* Nonzero if the sense of the character class is inverted. */
210 if (posixly_correct == 0)
211 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
216 if (*n == L_('.') && no_leading_period)
219 if (*n == L_('/') && (flags & FNM_FILE_NAME))
220 /* `/' cannot be matched. */
223 not = (*p == L_('!') || (posixly_correct < 0 && *p == L_('^')));
227 fn = FOLD ((UCHAR) *n);
232 if (!(flags & FNM_NOESCAPE) && c == L_('\\'))
236 c = FOLD ((UCHAR) *p);
241 else if (c == L_('[') && *p == L_(':'))
243 /* Leave room for the null. */
244 CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
246 #if defined _LIBC || WIDE_CHAR_SUPPORT
249 const CHAR *startp = p;
253 if (c1 == CHAR_CLASS_MAX_LENGTH)
254 /* The name is too long and therefore the pattern
259 if (c == L_(':') && p[1] == L_(']'))
264 if (c < L_('a') || c >= L_('z'))
266 /* This cannot possibly be a character class name.
267 Match it as a normal range. */
276 #if defined _LIBC || WIDE_CHAR_SUPPORT
277 wt = IS_CHAR_CLASS (str);
279 /* Invalid character class name. */
282 # if defined _LIBC && ! WIDE_CHAR_VERSION
283 /* The following code is glibc specific but does
284 there a good job in speeding up the code since
285 we can avoid the btowc() call. */
286 if (_ISCTYPE ((UCHAR) *n, wt))
289 if (ISWCTYPE (BTOWC ((UCHAR) *n), wt))
293 if ((STREQ (str, L_("alnum")) && isalnum ((UCHAR) *n))
294 || (STREQ (str, L_("alpha")) && isalpha ((UCHAR) *n))
295 || (STREQ (str, L_("blank")) && isblank ((UCHAR) *n))
296 || (STREQ (str, L_("cntrl")) && iscntrl ((UCHAR) *n))
297 || (STREQ (str, L_("digit")) && isdigit ((UCHAR) *n))
298 || (STREQ (str, L_("graph")) && isgraph ((UCHAR) *n))
299 || (STREQ (str, L_("lower")) && islower ((UCHAR) *n))
300 || (STREQ (str, L_("print")) && isprint ((UCHAR) *n))
301 || (STREQ (str, L_("punct")) && ispunct ((UCHAR) *n))
302 || (STREQ (str, L_("space")) && isspace ((UCHAR) *n))
303 || (STREQ (str, L_("upper")) && isupper ((UCHAR) *n))
304 || (STREQ (str, L_("xdigit")) && isxdigit ((UCHAR) *n)))
310 else if (c == L_('[') && *p == L_('='))
314 _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
315 const CHAR *startp = p;
327 if (c != L_('=') || p[1] != L_(']'))
337 if ((UCHAR) *n == str[0])
342 const int32_t *table;
343 # if WIDE_CHAR_VERSION
344 const int32_t *weights;
345 const int32_t *extra;
347 const unsigned char *weights;
348 const unsigned char *extra;
350 const int32_t *indirect;
352 const UCHAR *cp = (const UCHAR *) str;
354 /* This #include defines a local function! */
355 # if WIDE_CHAR_VERSION
356 # include <locale/weightwc.h>
358 # include <locale/weight.h>
361 # if WIDE_CHAR_VERSION
362 table = (const int32_t *)
363 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
364 weights = (const int32_t *)
365 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
366 extra = (const int32_t *)
367 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
368 indirect = (const int32_t *)
369 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
371 table = (const int32_t *)
372 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
373 weights = (const unsigned char *)
374 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
375 extra = (const unsigned char *)
376 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
377 indirect = (const int32_t *)
378 _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
384 /* We found a table entry. Now see whether the
385 character we are currently at has the same
386 equivalance class value. */
387 int len = weights[idx & 0xffffff];
389 const UCHAR *np = (const UCHAR *) n;
391 idx2 = findidx (&np);
393 && (idx >> 24) == (idx2 >> 24)
394 && len == weights[idx2 & 0xffffff])
402 && (weights[idx + 1 + cnt]
403 == weights[idx2 + 1 + cnt]))
415 else if (c == L_('\0'))
416 /* [ (unterminated) loses. */
420 bool is_range = false;
423 bool is_seqval = false;
425 if (c == L_('[') && *p == L_('.'))
428 _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
429 const CHAR *startp = p;
435 if (c == L_('.') && p[1] == L_(']'))
445 /* We have to handling the symbols differently in
446 ranges since then the collation sequence is
448 is_range = *p == L_('-') && p[1] != L_('\0');
452 /* There are no names defined in the collation
453 data. Therefore we only accept the trivial
454 names consisting of the character itself. */
458 if (!is_range && *n == startp[1])
467 const int32_t *symb_table;
468 # ifdef WIDE_CHAR_VERSION
472 # define str (startp + 1)
474 const unsigned char *extra;
480 # ifdef WIDE_CHAR_VERSION
481 /* We have to convert the name to a single-byte
482 string. This is possible since the names
483 consist of ASCII characters and the internal
484 representation is UCS4. */
485 for (strcnt = 0; strcnt < c1; ++strcnt)
486 str[strcnt] = startp[1 + strcnt];
490 _NL_CURRENT_WORD (LC_COLLATE,
491 _NL_COLLATE_SYMB_HASH_SIZEMB);
492 symb_table = (const int32_t *)
493 _NL_CURRENT (LC_COLLATE,
494 _NL_COLLATE_SYMB_TABLEMB);
495 extra = (const unsigned char *)
496 _NL_CURRENT (LC_COLLATE,
497 _NL_COLLATE_SYMB_EXTRAMB);
499 /* Locate the character in the hashing table. */
500 hash = elem_hash (str, c1);
503 elem = hash % table_size;
504 if (symb_table[2 * elem] != 0)
506 second = hash % (table_size - 2) + 1;
510 /* First compare the hashing value. */
511 if (symb_table[2 * elem] == hash
513 == extra[symb_table[2 * elem + 1]])
515 &extra[symb_table[2 * elem
519 /* Yep, this is the entry. */
520 idx = symb_table[2 * elem + 1];
521 idx += 1 + extra[idx];
528 while (symb_table[2 * elem] != 0);
531 if (symb_table[2 * elem] != 0)
533 /* Compare the byte sequence but only if
534 this is not part of a range. */
535 # ifdef WIDE_CHAR_VERSION
538 idx += 1 + extra[idx];
539 /* Adjust for the alignment. */
540 idx = (idx + 3) & ~3;
542 wextra = (int32_t *) &extra[idx + 4];
547 # ifdef WIDE_CHAR_VERSION
549 (int32_t) c1 < wextra[idx];
551 if (n[c1] != wextra[1 + c1])
554 if ((int32_t) c1 == wextra[idx])
557 for (c1 = 0; c1 < extra[idx]; ++c1)
558 if (n[c1] != extra[1 + c1])
561 if (c1 == extra[idx])
566 /* Get the collation sequence value. */
568 # ifdef WIDE_CHAR_VERSION
569 cold = wextra[1 + wextra[idx]];
571 /* Adjust for the alignment. */
572 idx += 1 + extra[idx];
573 idx = (idx + 3) & ~4;
574 cold = *((int32_t *) &extra[idx]);
581 /* No valid character. Match it as a
583 if (!is_range && *n == str[0])
600 /* We have to handling the symbols differently in
601 ranges since then the collation sequence is
603 is_range = (*p == L_('-') && p[1] != L_('\0')
606 if (!is_range && c == fn)
610 /* This is needed if we goto normal_bracket; from
611 outside of is_seqval's scope. */
619 if (c == L_('-') && *p != L_(']'))
622 /* We have to find the collation sequence
623 value for C. Collation sequence is nothing
624 we can regularly access. The sequence
625 value is defined by the order in which the
626 definitions of the collation values for the
627 various characters appear in the source
628 file. A strange concept, nowhere
634 # ifdef WIDE_CHAR_VERSION
635 /* Search in the `names' array for the characters. */
636 fcollseq = __collseq_table_lookup (collseq, fn);
637 if (fcollseq == ~((uint32_t) 0))
638 /* XXX We don't know anything about the character
639 we are supposed to match. This means we are
641 goto range_not_matched;
646 lcollseq = __collseq_table_lookup (collseq, cold);
648 fcollseq = collseq[fn];
649 lcollseq = is_seqval ? cold : collseq[(UCHAR) cold];
653 if (cend == L_('[') && *p == L_('.'))
656 _NL_CURRENT_WORD (LC_COLLATE,
658 const CHAR *startp = p;
664 if (c == L_('.') && p[1] == L_(']'))
676 /* There are no names defined in the
677 collation data. Therefore we only
678 accept the trivial names consisting
679 of the character itself. */
688 const int32_t *symb_table;
689 # ifdef WIDE_CHAR_VERSION
693 # define str (startp + 1)
695 const unsigned char *extra;
701 # ifdef WIDE_CHAR_VERSION
702 /* We have to convert the name to a single-byte
703 string. This is possible since the names
704 consist of ASCII characters and the internal
705 representation is UCS4. */
706 for (strcnt = 0; strcnt < c1; ++strcnt)
707 str[strcnt] = startp[1 + strcnt];
711 _NL_CURRENT_WORD (LC_COLLATE,
712 _NL_COLLATE_SYMB_HASH_SIZEMB);
713 symb_table = (const int32_t *)
714 _NL_CURRENT (LC_COLLATE,
715 _NL_COLLATE_SYMB_TABLEMB);
716 extra = (const unsigned char *)
717 _NL_CURRENT (LC_COLLATE,
718 _NL_COLLATE_SYMB_EXTRAMB);
720 /* Locate the character in the hashing
722 hash = elem_hash (str, c1);
725 elem = hash % table_size;
726 if (symb_table[2 * elem] != 0)
728 second = hash % (table_size - 2) + 1;
732 /* First compare the hashing value. */
733 if (symb_table[2 * elem] == hash
735 == extra[symb_table[2 * elem + 1]])
737 &extra[symb_table[2 * elem + 1]
740 /* Yep, this is the entry. */
741 idx = symb_table[2 * elem + 1];
742 idx += 1 + extra[idx];
749 while (symb_table[2 * elem] != 0);
752 if (symb_table[2 * elem] != 0)
754 /* Compare the byte sequence but only if
755 this is not part of a range. */
756 # ifdef WIDE_CHAR_VERSION
759 idx += 1 + extra[idx];
760 /* Adjust for the alignment. */
761 idx = (idx + 3) & ~4;
763 wextra = (int32_t *) &extra[idx + 4];
765 /* Get the collation sequence value. */
767 # ifdef WIDE_CHAR_VERSION
768 cend = wextra[1 + wextra[idx]];
770 /* Adjust for the alignment. */
771 idx += 1 + extra[idx];
772 idx = (idx + 3) & ~4;
773 cend = *((int32_t *) &extra[idx]);
776 else if (symb_table[2 * elem] != 0 && c1 == 1)
788 if (!(flags & FNM_NOESCAPE) && cend == L_('\\'))
790 if (cend == L_('\0'))
795 /* XXX It is not entirely clear to me how to handle
796 characters which are not mentioned in the
797 collation specification. */
799 # ifdef WIDE_CHAR_VERSION
800 lcollseq == 0xffffffff ||
802 lcollseq <= fcollseq)
804 /* We have to look at the upper bound. */
811 # ifdef WIDE_CHAR_VERSION
813 __collseq_table_lookup (collseq, cend);
814 if (hcollseq == ~((uint32_t) 0))
816 /* Hum, no information about the upper
817 bound. The matching succeeds if the
818 lower bound is matched exactly. */
819 if (lcollseq != fcollseq)
820 goto range_not_matched;
825 hcollseq = collseq[cend];
829 if (lcollseq <= hcollseq && fcollseq <= hcollseq)
832 # ifdef WIDE_CHAR_VERSION
836 /* We use a boring value comparison of the character
837 values. This is better than comparing using
838 `strcoll' since the latter would have surprising
839 and sometimes fatal consequences. */
842 if (!(flags & FNM_NOESCAPE) && cend == L_('\\'))
844 if (cend == L_('\0'))
848 if (cold <= fn && fn <= cend)
865 /* Skip the rest of the [...] that already matched. */
872 /* [... (unterminated) loses. */
875 if (!(flags & FNM_NOESCAPE) && c == L_('\\'))
879 /* XXX 1003.2d11 is unclear if this is right. */
882 else if (c == L_('[') && *p == L_(':'))
885 const CHAR *startp = p;
890 if (++c1 == CHAR_CLASS_MAX_LENGTH)
893 if (*p == L_(':') && p[1] == L_(']'))
896 if (c < L_('a') || c >= L_('z'))
905 else if (c == L_('[') && *p == L_('='))
911 if (c != L_('=') || p[1] != L_(']'))
916 else if (c == L_('[') && *p == L_('.'))
925 if (*p == L_('.') && p[1] == L_(']'))
932 while (c != L_(']'));
941 if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
945 res = EXT (c, p, n, string_end, no_leading_period, flags);
952 if (NO_LEADING_PERIOD (flags))
954 if (n == string_end || c != (UCHAR) *n)
957 new_no_leading_period = true;
963 if (n == string_end || c != FOLD ((UCHAR) *n))
967 no_leading_period = new_no_leading_period;
974 if ((flags & FNM_LEADING_DIR) && n != string_end && *n == L_('/'))
975 /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
984 END (const CHAR *pattern)
986 const CHAR *p = pattern;
989 if (*++p == L_('\0'))
990 /* This is an invalid pattern. */
992 else if (*p == L_('['))
994 /* Handle brackets special. */
995 if (posixly_correct == 0)
996 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
998 /* Skip the not sign. We have to recognize it because of a possibly
1000 if (*++p == L_('!') || (posixly_correct < 0 && *p == L_('^')))
1002 /* A leading ']' is recognized as such. */
1005 /* Skip over all characters of the list. */
1006 while (*p != L_(']'))
1007 if (*p++ == L_('\0'))
1008 /* This is no valid pattern. */
1011 else if ((*p == L_('?') || *p == L_('*') || *p == L_('+') || *p == L_('@')
1012 || *p == L_('!')) && p[1] == L_('('))
1014 else if (*p == L_(')'))
1023 EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end,
1024 bool no_leading_period, int flags)
1030 struct patternlist *next;
1033 struct patternlist **lastp = &list;
1034 size_t pattern_len = STRLEN (pattern);
1037 enum { ALLOCA_LIMIT = 8000 };
1039 /* Parse the pattern. Store the individual parts in the list. */
1041 for (startp = p = pattern + 1; ; ++p)
1043 /* This is an invalid pattern. */
1045 else if (*p == L_('['))
1047 /* Handle brackets special. */
1048 if (posixly_correct == 0)
1049 posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
1051 /* Skip the not sign. We have to recognize it because of a possibly
1053 if (*++p == L_('!') || (posixly_correct < 0 && *p == L_('^')))
1055 /* A leading ']' is recognized as such. */
1058 /* Skip over all characters of the list. */
1059 while (*p != L_(']'))
1060 if (*p++ == L_('\0'))
1061 /* This is no valid pattern. */
1064 else if ((*p == L_('?') || *p == L_('*') || *p == L_('+') || *p == L_('@')
1065 || *p == L_('!')) && p[1] == L_('('))
1066 /* Remember the nesting level. */
1068 else if (*p == L_(')'))
1072 /* This means we found the end of the pattern. */
1073 #define NEW_PATTERN \
1074 struct patternlist *newp; \
1079 plen = (opt == L_('?') || opt == L_('@') \
1081 : p - startp + 1UL); \
1082 plensize = plen * sizeof (CHAR); \
1083 newpsize = offsetof (struct patternlist, str) + plensize; \
1084 if ((size_t) -1 / sizeof (CHAR) < plen \
1085 || newpsize < offsetof (struct patternlist, str) \
1086 || ALLOCA_LIMIT <= newpsize) \
1088 newp = (struct patternlist *) alloca (newpsize); \
1089 *((CHAR *) MEMPCPY (newp->str, startp, p - startp)) = L_('\0'); \
1090 newp->next = NULL; \
1097 else if (*p == L_('|'))
1105 assert (list != NULL);
1106 assert (p[-1] == L_(')'));
1112 if (FCT (p, string, string_end, no_leading_period, flags) == 0)
1119 for (rs = string; rs <= string_end; ++rs)
1120 /* First match the prefix with the current pattern with the
1122 if (FCT (list->str, string, rs, no_leading_period,
1123 flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0
1124 /* This was successful. Now match the rest with the rest
1126 && (FCT (p, rs, string_end,
1129 : rs[-1] == '/' && NO_LEADING_PERIOD (flags),
1130 flags & FNM_FILE_NAME
1131 ? flags : flags & ~FNM_PERIOD) == 0
1132 /* This didn't work. Try the whole pattern. */
1134 && FCT (pattern - 1, rs, string_end,
1137 : rs[-1] == '/' && NO_LEADING_PERIOD (flags),
1138 flags & FNM_FILE_NAME
1139 ? flags : flags & ~FNM_PERIOD) == 0)))
1140 /* It worked. Signal success. */
1143 while ((list = list->next) != NULL);
1145 /* None of the patterns lead to a match. */
1149 if (FCT (p, string, string_end, no_leading_period, flags) == 0)
1155 /* I cannot believe it but `strcat' is actually acceptable
1156 here. Match the entire string with the prefix from the
1157 pattern list and the rest of the pattern following the
1159 if (FCT (STRCAT (list->str, p), string, string_end,
1161 flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
1162 /* It worked. Signal success. */
1164 while ((list = list->next) != NULL);
1166 /* None of the patterns lead to a match. */
1170 for (rs = string; rs <= string_end; ++rs)
1172 struct patternlist *runp;
1174 for (runp = list; runp != NULL; runp = runp->next)
1175 if (FCT (runp->str, string, rs, no_leading_period,
1176 flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
1179 /* If none of the patterns matched see whether the rest does. */
1181 && (FCT (p, rs, string_end,
1184 : rs[-1] == '/' && NO_LEADING_PERIOD (flags),
1185 flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD)
1187 /* This is successful. */
1191 /* None of the patterns together with the rest of the pattern
1196 assert (! "Invalid extended matching operator");