aboutsummaryrefslogtreecommitdiff
path: root/posix/fnmatch_loop.c
diff options
context:
space:
mode:
Diffstat (limited to 'posix/fnmatch_loop.c')
-rw-r--r--posix/fnmatch_loop.c1977
1 files changed, 985 insertions, 992 deletions
diff --git a/posix/fnmatch_loop.c b/posix/fnmatch_loop.c
index 1b2f6f05e1..7f938af590 100644
--- a/posix/fnmatch_loop.c
+++ b/posix/fnmatch_loop.c
@@ -15,28 +15,30 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
-#include <stdint.h>
+#ifdef _LIBC
+# include <stdint.h>
+#endif
struct STRUCT
{
const CHAR *pattern;
const CHAR *string;
- int no_leading_period;
+ bool no_leading_period;
};
-/* Match STRING against the filename pattern PATTERN, returning zero if
+/* Match STRING against the file name pattern PATTERN, returning zero if
it matches, nonzero if not. */
static int FCT (const CHAR *pattern, const CHAR *string,
- const CHAR *string_end, int no_leading_period, int flags,
- struct STRUCT *ends, size_t alloca_used);
+ const CHAR *string_end, bool no_leading_period, int flags,
+ struct STRUCT *ends, size_t alloca_used);
static int EXT (INT opt, const CHAR *pattern, const CHAR *string,
- const CHAR *string_end, int no_leading_period, int flags,
- size_t alloca_used);
+ const CHAR *string_end, bool no_leading_period, int flags,
+ size_t alloca_used);
static const CHAR *END (const CHAR *patternp);
static int
FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end,
- int no_leading_period, int flags, struct STRUCT *ends, size_t alloca_used)
+ bool no_leading_period, int flags, struct STRUCT *ends, size_t alloca_used)
{
const CHAR *p = pattern, *n = string;
UCHAR c;
@@ -50,882 +52,868 @@ FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end,
# endif
#endif
- while ((c = *p++) != L('\0'))
+ while ((c = *p++) != L_('\0'))
{
- int new_no_leading_period = 0;
+ bool new_no_leading_period = false;
c = FOLD (c);
switch (c)
- {
- case L('?'):
- if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
- {
- int res = EXT (c, p, n, string_end, no_leading_period,
- flags, alloca_used);
- if (res != -1)
- return res;
- }
-
- if (n == string_end)
- return FNM_NOMATCH;
- else if (*n == L('/') && (flags & FNM_FILE_NAME))
- return FNM_NOMATCH;
- else if (*n == L('.') && no_leading_period)
- return FNM_NOMATCH;
- break;
-
- case L('\\'):
- if (!(flags & FNM_NOESCAPE))
- {
- c = *p++;
- if (c == L('\0'))
- /* Trailing \ loses. */
- return FNM_NOMATCH;
- c = FOLD (c);
- }
- if (n == string_end || FOLD ((UCHAR) *n) != c)
- return FNM_NOMATCH;
- break;
-
- case L('*'):
- if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
- {
- int res = EXT (c, p, n, string_end, no_leading_period,
- flags, alloca_used);
- if (res != -1)
- return res;
- }
- else if (ends != NULL)
- {
- ends->pattern = p - 1;
- ends->string = n;
- ends->no_leading_period = no_leading_period;
- return 0;
- }
-
- if (n != string_end && *n == L('.') && no_leading_period)
- return FNM_NOMATCH;
-
- for (c = *p++; c == L('?') || c == L('*'); c = *p++)
- {
- if (*p == L('(') && (flags & FNM_EXTMATCH) != 0)
- {
- const CHAR *endp = END (p);
- if (endp != p)
- {
- /* This is a pattern. Skip over it. */
- p = endp;
- continue;
- }
- }
-
- if (c == L('?'))
- {
- /* A ? needs to match one character. */
- if (n == string_end)
- /* There isn't another character; no match. */
- return FNM_NOMATCH;
- else if (*n == L('/')
- && __builtin_expect (flags & FNM_FILE_NAME, 0))
- /* A slash does not match a wildcard under
- FNM_FILE_NAME. */
- return FNM_NOMATCH;
- else
- /* One character of the string is consumed in matching
- this ? wildcard, so *??? won't match if there are
- less than three characters. */
- ++n;
- }
- }
-
- if (c == L('\0'))
- /* The wildcard(s) is/are the last element of the pattern.
- If the name is a file name and contains another slash
- this means it cannot match, unless the FNM_LEADING_DIR
- flag is set. */
- {
- int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
-
- if (flags & FNM_FILE_NAME)
- {
- if (flags & FNM_LEADING_DIR)
- result = 0;
- else
- {
- if (MEMCHR (n, L('/'), string_end - n) == NULL)
- result = 0;
- }
- }
-
- return result;
- }
- else
- {
- const CHAR *endp;
- struct STRUCT end;
-
- end.pattern = NULL;
- endp = MEMCHR (n, (flags & FNM_FILE_NAME) ? L('/') : L('\0'),
- string_end - n);
- if (endp == NULL)
- endp = string_end;
-
- if (c == L('[')
- || (__builtin_expect (flags & FNM_EXTMATCH, 0) != 0
- && (c == L('@') || c == L('+') || c == L('!'))
- && *p == L('(')))
- {
- int flags2 = ((flags & FNM_FILE_NAME)
- ? flags : (flags & ~FNM_PERIOD));
-
- for (--p; n < endp; ++n, no_leading_period = 0)
- if (FCT (p, n, string_end, no_leading_period, flags2,
- &end, alloca_used) == 0)
- goto found;
- }
- else if (c == L('/') && (flags & FNM_FILE_NAME))
- {
- while (n < string_end && *n != L('/'))
- ++n;
- if (n < string_end && *n == L('/')
- && (FCT (p, n + 1, string_end, flags & FNM_PERIOD, flags,
- NULL, alloca_used) == 0))
- return 0;
- }
- else
- {
- int flags2 = ((flags & FNM_FILE_NAME)
- ? flags : (flags & ~FNM_PERIOD));
-
- if (c == L('\\') && !(flags & FNM_NOESCAPE))
- c = *p;
- c = FOLD (c);
- for (--p; n < endp; ++n, no_leading_period = 0)
- if (FOLD ((UCHAR) *n) == c
- && (FCT (p, n, string_end, no_leading_period, flags2,
- &end, alloca_used) == 0))
- {
- found:
- if (end.pattern == NULL)
- return 0;
- break;
- }
- if (end.pattern != NULL)
- {
- p = end.pattern;
- n = end.string;
- no_leading_period = end.no_leading_period;
- continue;
- }
- }
- }
-
- /* If we come here no match is possible with the wildcard. */
- return FNM_NOMATCH;
-
- case L('['):
- {
- /* Nonzero if the sense of the character class is inverted. */
- const CHAR *p_init = p;
- const CHAR *n_init = n;
- int not;
- CHAR cold;
- UCHAR fn;
-
- if (posixly_correct == 0)
- posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
-
- if (n == string_end)
- return FNM_NOMATCH;
-
- if (*n == L('.') && no_leading_period)
- return FNM_NOMATCH;
-
- if (*n == L('/') && (flags & FNM_FILE_NAME))
- /* `/' cannot be matched. */
- return FNM_NOMATCH;
-
- not = (*p == L('!') || (posixly_correct < 0 && *p == L('^')));
- if (not)
- ++p;
-
- fn = FOLD ((UCHAR) *n);
-
- c = *p++;
- for (;;)
- {
- if (!(flags & FNM_NOESCAPE) && c == L('\\'))
- {
- if (*p == L('\0'))
- return FNM_NOMATCH;
- c = FOLD ((UCHAR) *p);
- ++p;
-
- goto normal_bracket;
- }
- else if (c == L('[') && *p == L(':'))
- {
- /* Leave room for the null. */
- CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
- size_t c1 = 0;
-#if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
- wctype_t wt;
-#endif
- const CHAR *startp = p;
-
- for (;;)
- {
- if (c1 == CHAR_CLASS_MAX_LENGTH)
- /* The name is too long and therefore the pattern
- is ill-formed. */
- return FNM_NOMATCH;
-
- c = *++p;
- if (c == L(':') && p[1] == L(']'))
- {
- p += 2;
- break;
- }
- if (c < L('a') || c >= L('z'))
- {
- /* This cannot possibly be a character class name.
- Match it as a normal range. */
- p = startp;
- c = L('[');
- goto normal_bracket;
- }
- str[c1++] = c;
- }
- str[c1] = L('\0');
-
-#if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
- wt = IS_CHAR_CLASS (str);
- if (wt == 0)
- /* Invalid character class name. */
- return FNM_NOMATCH;
-
-# if defined _LIBC && ! WIDE_CHAR_VERSION
- /* The following code is glibc specific but does
- there a good job in speeding up the code since
- we can avoid the btowc() call. */
- if (_ISCTYPE ((UCHAR) *n, wt))
- goto matched;
-# else
- if (ISWCTYPE (BTOWC ((UCHAR) *n), wt))
- goto matched;
-# endif
+ {
+ case L_('?'):
+ if (__glibc_unlikely (flags & FNM_EXTMATCH) && *p == '(')
+ {
+ int res = EXT (c, p, n, string_end, no_leading_period,
+ flags, alloca_used);
+ if (res != -1)
+ return res;
+ }
+
+ if (n == string_end)
+ return FNM_NOMATCH;
+ else if (*n == L_('/') && (flags & FNM_FILE_NAME))
+ return FNM_NOMATCH;
+ else if (*n == L_('.') && no_leading_period)
+ return FNM_NOMATCH;
+ break;
+
+ case L_('\\'):
+ if (!(flags & FNM_NOESCAPE))
+ {
+ c = *p++;
+ if (c == L_('\0'))
+ /* Trailing \ loses. */
+ return FNM_NOMATCH;
+ c = FOLD (c);
+ }
+ if (n == string_end || FOLD ((UCHAR) *n) != c)
+ return FNM_NOMATCH;
+ break;
+
+ case L_('*'):
+ if (__glibc_unlikely (flags & FNM_EXTMATCH) && *p == '(')
+ {
+ int res = EXT (c, p, n, string_end, no_leading_period,
+ flags, alloca_used);
+ if (res != -1)
+ return res;
+ }
+ else if (ends != NULL)
+ {
+ ends->pattern = p - 1;
+ ends->string = n;
+ ends->no_leading_period = no_leading_period;
+ return 0;
+ }
+
+ if (n != string_end && *n == L_('.') && no_leading_period)
+ return FNM_NOMATCH;
+
+ for (c = *p++; c == L_('?') || c == L_('*'); c = *p++)
+ {
+ if (*p == L_('(') && (flags & FNM_EXTMATCH) != 0)
+ {
+ const CHAR *endp = END (p);
+ if (endp != p)
+ {
+ /* This is a pattern. Skip over it. */
+ p = endp;
+ continue;
+ }
+ }
+
+ if (c == L_('?'))
+ {
+ /* A ? needs to match one character. */
+ if (n == string_end)
+ /* There isn't another character; no match. */
+ return FNM_NOMATCH;
+ else if (*n == L_('/')
+ && __glibc_unlikely (flags & FNM_FILE_NAME))
+ /* A slash does not match a wildcard under
+ FNM_FILE_NAME. */
+ return FNM_NOMATCH;
+ else
+ /* One character of the string is consumed in matching
+ this ? wildcard, so *??? won't match if there are
+ less than three characters. */
+ ++n;
+ }
+ }
+
+ if (c == L_('\0'))
+ /* The wildcard(s) is/are the last element of the pattern.
+ If the name is a file name and contains another slash
+ this means it cannot match, unless the FNM_LEADING_DIR
+ flag is set. */
+ {
+ int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
+
+ if (flags & FNM_FILE_NAME)
+ {
+ if (flags & FNM_LEADING_DIR)
+ result = 0;
+ else
+ {
+ if (MEMCHR (n, L_('/'), string_end - n) == NULL)
+ result = 0;
+ }
+ }
+
+ return result;
+ }
+ else
+ {
+ const CHAR *endp;
+ struct STRUCT end;
+
+ end.pattern = NULL;
+ endp = MEMCHR (n, (flags & FNM_FILE_NAME) ? L_('/') : L_('\0'),
+ string_end - n);
+ if (endp == NULL)
+ endp = string_end;
+
+ if (c == L_('[')
+ || (__glibc_unlikely (flags & FNM_EXTMATCH)
+ && (c == L_('@') || c == L_('+') || c == L_('!'))
+ && *p == L_('(')))
+ {
+ int flags2 = ((flags & FNM_FILE_NAME)
+ ? flags : (flags & ~FNM_PERIOD));
+
+ for (--p; n < endp; ++n, no_leading_period = false)
+ if (FCT (p, n, string_end, no_leading_period, flags2,
+ &end, alloca_used) == 0)
+ goto found;
+ }
+ else if (c == L_('/') && (flags & FNM_FILE_NAME))
+ {
+ while (n < string_end && *n != L_('/'))
+ ++n;
+ if (n < string_end && *n == L_('/')
+ && (FCT (p, n + 1, string_end, flags & FNM_PERIOD, flags,
+ NULL, alloca_used) == 0))
+ return 0;
+ }
+ else
+ {
+ int flags2 = ((flags & FNM_FILE_NAME)
+ ? flags : (flags & ~FNM_PERIOD));
+
+ if (c == L_('\\') && !(flags & FNM_NOESCAPE))
+ c = *p;
+ c = FOLD (c);
+ for (--p; n < endp; ++n, no_leading_period = false)
+ if (FOLD ((UCHAR) *n) == c
+ && (FCT (p, n, string_end, no_leading_period, flags2,
+ &end, alloca_used) == 0))
+ {
+ found:
+ if (end.pattern == NULL)
+ return 0;
+ break;
+ }
+ if (end.pattern != NULL)
+ {
+ p = end.pattern;
+ n = end.string;
+ no_leading_period = end.no_leading_period;
+ continue;
+ }
+ }
+ }
+
+ /* If we come here no match is possible with the wildcard. */
+ return FNM_NOMATCH;
+
+ case L_('['):
+ {
+ /* Nonzero if the sense of the character class is inverted. */
+ const CHAR *p_init = p;
+ const CHAR *n_init = n;
+ bool not;
+ CHAR cold;
+ UCHAR fn;
+
+ if (posixly_correct == 0)
+ posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
+
+ if (n == string_end)
+ return FNM_NOMATCH;
+
+ if (*n == L_('.') && no_leading_period)
+ return FNM_NOMATCH;
+
+ if (*n == L_('/') && (flags & FNM_FILE_NAME))
+ /* '/' cannot be matched. */
+ return FNM_NOMATCH;
+
+ not = (*p == L_('!') || (posixly_correct < 0 && *p == L_('^')));
+ if (not)
+ ++p;
+
+ fn = FOLD ((UCHAR) *n);
+
+ c = *p++;
+ for (;;)
+ {
+ if (!(flags & FNM_NOESCAPE) && c == L_('\\'))
+ {
+ if (*p == L_('\0'))
+ return FNM_NOMATCH;
+ c = FOLD ((UCHAR) *p);
+ ++p;
+
+ goto normal_bracket;
+ }
+ else if (c == L_('[') && *p == L_(':'))
+ {
+ /* Leave room for the null. */
+ CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
+ size_t c1 = 0;
+ wctype_t wt;
+ const CHAR *startp = p;
+
+ for (;;)
+ {
+ if (c1 == CHAR_CLASS_MAX_LENGTH)
+ /* The name is too long and therefore the pattern
+ is ill-formed. */
+ return FNM_NOMATCH;
+
+ c = *++p;
+ if (c == L_(':') && p[1] == L_(']'))
+ {
+ p += 2;
+ break;
+ }
+ if (c < L_('a') || c >= L_('z'))
+ {
+ /* This cannot possibly be a character class name.
+ Match it as a normal range. */
+ p = startp;
+ c = L_('[');
+ goto normal_bracket;
+ }
+ str[c1++] = c;
+ }
+ str[c1] = L_('\0');
+
+ wt = IS_CHAR_CLASS (str);
+ if (wt == 0)
+ /* Invalid character class name. */
+ return FNM_NOMATCH;
+
+#if defined _LIBC && ! WIDE_CHAR_VERSION
+ /* The following code is glibc specific but does
+ there a good job in speeding up the code since
+ we can avoid the btowc() call. */
+ if (_ISCTYPE ((UCHAR) *n, wt))
+ goto matched;
#else
- if ((STREQ (str, L("alnum")) && ISALNUM ((UCHAR) *n))
- || (STREQ (str, L("alpha")) && ISALPHA ((UCHAR) *n))
- || (STREQ (str, L("blank")) && ISBLANK ((UCHAR) *n))
- || (STREQ (str, L("cntrl")) && ISCNTRL ((UCHAR) *n))
- || (STREQ (str, L("digit")) && ISDIGIT ((UCHAR) *n))
- || (STREQ (str, L("graph")) && ISGRAPH ((UCHAR) *n))
- || (STREQ (str, L("lower")) && ISLOWER ((UCHAR) *n))
- || (STREQ (str, L("print")) && ISPRINT ((UCHAR) *n))
- || (STREQ (str, L("punct")) && ISPUNCT ((UCHAR) *n))
- || (STREQ (str, L("space")) && ISSPACE ((UCHAR) *n))
- || (STREQ (str, L("upper")) && ISUPPER ((UCHAR) *n))
- || (STREQ (str, L("xdigit")) && ISXDIGIT ((UCHAR) *n)))
- goto matched;
+ if (iswctype (BTOWC ((UCHAR) *n), wt))
+ goto matched;
#endif
- c = *p++;
- }
+ c = *p++;
+ }
#ifdef _LIBC
- else if (c == L('[') && *p == L('='))
- {
- /* It's important that STR be a scalar variable rather
- than a one-element array, because GCC (at least 4.9.2
- -O2 on x86-64) can be confused by the array and
- diagnose a "used initialized" in a dead branch in the
- findidx function. */
- UCHAR str;
- uint32_t nrules =
- _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
- const CHAR *startp = p;
-
- c = *++p;
- if (c == L('\0'))
- {
- p = startp;
- c = L('[');
- goto normal_bracket;
- }
- str = c;
-
- c = *++p;
- if (c != L('=') || p[1] != L(']'))
- {
- p = startp;
- c = L('[');
- goto normal_bracket;
- }
- p += 2;
-
- if (nrules == 0)
- {
- if ((UCHAR) *n == str)
- goto matched;
- }
- else
- {
- const int32_t *table;
+ else if (c == L_('[') && *p == L_('='))
+ {
+ /* It's important that STR be a scalar variable rather
+ than a one-element array, because GCC (at least 4.9.2
+ -O2 on x86-64) can be confused by the array and
+ diagnose a "used initialized" in a dead branch in the
+ findidx function. */
+ UCHAR str;
+ uint32_t nrules =
+ _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
+ const CHAR *startp = p;
+
+ c = *++p;
+ if (c == L_('\0'))
+ {
+ p = startp;
+ c = L_('[');
+ goto normal_bracket;
+ }
+ str = c;
+
+ c = *++p;
+ if (c != L_('=') || p[1] != L_(']'))
+ {
+ p = startp;
+ c = L_('[');
+ goto normal_bracket;
+ }
+ p += 2;
+
+ if (nrules == 0)
+ {
+ if ((UCHAR) *n == str)
+ goto matched;
+ }
+ else
+ {
+ const int32_t *table;
# if WIDE_CHAR_VERSION
- const int32_t *weights;
- const wint_t *extra;
+ const int32_t *weights;
+ const wint_t *extra;
# else
- const unsigned char *weights;
- const unsigned char *extra;
+ const unsigned char *weights;
+ const unsigned char *extra;
# endif
- const int32_t *indirect;
- int32_t idx;
- const UCHAR *cp = (const UCHAR *) &str;
+ const int32_t *indirect;
+ int32_t idx;
+ const UCHAR *cp = (const UCHAR *) &str;
# if WIDE_CHAR_VERSION
- table = (const int32_t *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
- weights = (const int32_t *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
- extra = (const wint_t *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
- indirect = (const int32_t *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
+ table = (const int32_t *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
+ weights = (const int32_t *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
+ extra = (const wint_t *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
+ indirect = (const int32_t *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
# else
- table = (const int32_t *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
- weights = (const unsigned char *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
- extra = (const unsigned char *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
- indirect = (const int32_t *)
- _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
+ table = (const int32_t *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
+ weights = (const unsigned char *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
+ extra = (const unsigned char *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
+ indirect = (const int32_t *)
+ _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
# endif
- idx = FINDIDX (table, indirect, extra, &cp, 1);
- if (idx != 0)
- {
- /* We found a table entry. Now see whether the
- character we are currently at has the same
- equivalance class value. */
- int len = weights[idx & 0xffffff];
- int32_t idx2;
- const UCHAR *np = (const UCHAR *) n;
-
- idx2 = FINDIDX (table, indirect, extra,
- &