| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef _LIBC |
| 17 |
# include <config.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
#ifndef _GNU_SOURCE |
| 22 |
# define _GNU_SOURCE 1 |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <fnmatch.h> |
| 26 |
|
| 27 |
#include <alloca.h> |
| 28 |
#include <assert.h> |
| 29 |
#include <ctype.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <stddef.h> |
| 32 |
#include <stdbool.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
|
| 36 |
#define WIDE_CHAR_SUPPORT \ |
| 37 |
(HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \ |
| 38 |
&& HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY)) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#if defined _LIBC || WIDE_CHAR_SUPPORT |
| 43 |
# include <wctype.h> |
| 44 |
# include <wchar.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#ifdef _LIBC |
| 51 |
# include "../locale/localeinfo.h" |
| 52 |
# include "../locale/elem-hash.h" |
| 53 |
# include "../locale/coll-lookup.h" |
| 54 |
# include <shlib-compat.h> |
| 55 |
|
| 56 |
# define CONCAT(a,b) __CONCAT(a,b) |
| 57 |
# define mbsrtowcs __mbsrtowcs |
| 58 |
# define fnmatch __fnmatch |
| 59 |
extern int fnmatch (const char *pattern, const char *string, int flags); |
| 60 |
#endif |
| 61 |
|
| 62 |
#ifndef SIZE_MAX |
| 63 |
# define SIZE_MAX ((size_t) -1) |
| 64 |
#endif |
| 65 |
|
| 66 |
#include "flexmember.h" |
| 67 |
|
| 68 |
#ifndef FALLTHROUGH |
| 69 |
# if __GNUC__ < 7 |
| 70 |
# define FALLTHROUGH ((void) 0) |
| 71 |
# else |
| 72 |
# define FALLTHROUGH __attribute__ ((__fallthrough__)) |
| 73 |
# endif |
| 74 |
#endif |
| 75 |
|
| 76 |
|
| 77 |
#define NO_LEADING_PERIOD(flags) \ |
| 78 |
((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD)) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
#if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU |
| 90 |
|
| 91 |
|
| 92 |
# if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK)) |
| 93 |
# define isblank(c) ((c) == ' ' || (c) == '\t') |
| 94 |
# endif |
| 95 |
|
| 96 |
# define STREQ(s1, s2) (strcmp (s1, s2) == 0) |
| 97 |
|
| 98 |
# if defined _LIBC || WIDE_CHAR_SUPPORT |
| 99 |
|
| 100 |
|
| 101 |
# ifdef CHARCLASS_NAME_MAX |
| 102 |
# define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX |
| 103 |
# else |
| 104 |
|
| 105 |
|
| 106 |
# define CHAR_CLASS_MAX_LENGTH 256 |
| 107 |
# endif |
| 108 |
|
| 109 |
# ifdef _LIBC |
| 110 |
# define IS_CHAR_CLASS(string) __wctype (string) |
| 111 |
# else |
| 112 |
# define IS_CHAR_CLASS(string) wctype (string) |
| 113 |
# endif |
| 114 |
|
| 115 |
# ifdef _LIBC |
| 116 |
# define ISWCTYPE(WC, WT) __iswctype (WC, WT) |
| 117 |
# else |
| 118 |
# define ISWCTYPE(WC, WT) iswctype (WC, WT) |
| 119 |
# endif |
| 120 |
|
| 121 |
# if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC |
| 122 |
|
| 123 |
# define HANDLE_MULTIBYTE 1 |
| 124 |
# endif |
| 125 |
|
| 126 |
# else |
| 127 |
# define CHAR_CLASS_MAX_LENGTH 6 |
| 128 |
|
| 129 |
# define IS_CHAR_CLASS(string) \ |
| 130 |
(STREQ (string, "alpha") || STREQ (string, "upper") \ |
| 131 |
|| STREQ (string, "lower") || STREQ (string, "digit") \ |
| 132 |
|| STREQ (string, "alnum") || STREQ (string, "xdigit") \ |
| 133 |
|| STREQ (string, "space") || STREQ (string, "print") \ |
| 134 |
|| STREQ (string, "punct") || STREQ (string, "graph") \ |
| 135 |
|| STREQ (string, "cntrl") || STREQ (string, "blank")) |
| 136 |
# endif |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
static int posixly_correct; |
| 143 |
|
| 144 |
# ifndef internal_function |
| 145 |
|
| 146 |
|
| 147 |
# define internal_function |
| 148 |
# endif |
| 149 |
|
| 150 |
|
| 151 |
# define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c)) |
| 152 |
# define CHAR char |
| 153 |
# define UCHAR unsigned char |
| 154 |
# define INT int |
| 155 |
# define FCT internal_fnmatch |
| 156 |
# define EXT ext_match |
| 157 |
# define END end_pattern |
| 158 |
# define L_(CS) CS |
| 159 |
# ifdef _LIBC |
| 160 |
# define BTOWC(C) __btowc (C) |
| 161 |
# else |
| 162 |
# define BTOWC(C) btowc (C) |
| 163 |
# endif |
| 164 |
# define STRLEN(S) strlen (S) |
| 165 |
# define STRCAT(D, S) strcat (D, S) |
| 166 |
# ifdef _LIBC |
| 167 |
# define MEMPCPY(D, S, N) __mempcpy (D, S, N) |
| 168 |
# else |
| 169 |
# if HAVE_MEMPCPY |
| 170 |
# define MEMPCPY(D, S, N) mempcpy (D, S, N) |
| 171 |
# else |
| 172 |
# define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N))) |
| 173 |
# endif |
| 174 |
# endif |
| 175 |
# define MEMCHR(S, C, N) memchr (S, C, N) |
| 176 |
# include "fnmatch_loop.c" |
| 177 |
|
| 178 |
|
| 179 |
# if HANDLE_MULTIBYTE |
| 180 |
# define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c)) |
| 181 |
# define CHAR wchar_t |
| 182 |
# define UCHAR wint_t |
| 183 |
# define INT wint_t |
| 184 |
# define FCT internal_fnwmatch |
| 185 |
# define EXT ext_wmatch |
| 186 |
# define END end_wpattern |
| 187 |
# define L_(CS) L##CS |
| 188 |
# define BTOWC(C) (C) |
| 189 |
# ifdef _LIBC |
| 190 |
# define STRLEN(S) __wcslen (S) |
| 191 |
# define STRCAT(D, S) __wcscat (D, S) |
| 192 |
# define MEMPCPY(D, S, N) __wmempcpy (D, S, N) |
| 193 |
# else |
| 194 |
# define STRLEN(S) wcslen (S) |
| 195 |
# define STRCAT(D, S) wcscat (D, S) |
| 196 |
# if HAVE_WMEMPCPY |
| 197 |
# define MEMPCPY(D, S, N) wmempcpy (D, S, N) |
| 198 |
# else |
| 199 |
# define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N)) |
| 200 |
# endif |
| 201 |
# endif |
| 202 |
# define MEMCHR(S, C, N) wmemchr (S, C, N) |
| 203 |
# define WIDE_CHAR_VERSION 1 |
| 204 |
|
| 205 |
# undef IS_CHAR_CLASS |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static wctype_t |
| 213 |
is_char_class (const wchar_t *wcs) |
| 214 |
{ |
| 215 |
char s[CHAR_CLASS_MAX_LENGTH + 1]; |
| 216 |
char *cp = s; |
| 217 |
|
| 218 |
do |
| 219 |
{ |
| 220 |
|
| 221 |
# ifdef _LIBC |
| 222 |
if (*wcs < 0x20 || *wcs > 0x7e |
| 223 |
|| *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60) |
| 224 |
return (wctype_t) 0; |
| 225 |
# else |
| 226 |
switch (*wcs) |
| 227 |
{ |
| 228 |
case L' ': case L'!': case L'"': case L'#': case L'%': |
| 229 |
case L'&': case L'\'': case L'(': case L')': case L'*': |
| 230 |
case L'+': case L',': case L'-': case L'.': case L'/': |
| 231 |
case L'0': case L'1': case L'2': case L'3': case L'4': |
| 232 |
case L'5': case L'6': case L'7': case L'8': case L'9': |
| 233 |
case L':': case L';': case L'<': case L'=': case L'>': |
| 234 |
case L'?': |
| 235 |
case L'A': case L'B': case L'C': case L'D': case L'E': |
| 236 |
case L'F': case L'G': case L'H': case L'I': case L'J': |
| 237 |
case L'K': case L'L': case L'M': case L'N': case L'O': |
| 238 |
case L'P': case L'Q': case L'R': case L'S': case L'T': |
| 239 |
case L'U': case L'V': case L'W': case L'X': case L'Y': |
| 240 |
case L'Z': |
| 241 |
case L'[': case L'\\': case L']': case L'^': case L'_': |
| 242 |
case L'a': case L'b': case L'c': case L'd': case L'e': |
| 243 |
case L'f': case L'g': case L'h': case L'i': case L'j': |
| 244 |
case L'k': case L'l': case L'm': case L'n': case L'o': |
| 245 |
case L'p': case L'q': case L'r': case L's': case L't': |
| 246 |
case L'u': case L'v': case L'w': case L'x': case L'y': |
| 247 |
case L'z': case L'{': case L'|': case L'}': case L'~': |
| 248 |
break; |
| 249 |
default: |
| 250 |
return (wctype_t) 0; |
| 251 |
} |
| 252 |
# endif |
| 253 |
|
| 254 |
|
| 255 |
if (cp == s + CHAR_CLASS_MAX_LENGTH) |
| 256 |
return (wctype_t) 0; |
| 257 |
|
| 258 |
*cp++ = (char) *wcs++; |
| 259 |
} |
| 260 |
while (*wcs != L'\0'); |
| 261 |
|
| 262 |
*cp = '\0'; |
| 263 |
|
| 264 |
# ifdef _LIBC |
| 265 |
return __wctype (s); |
| 266 |
# else |
| 267 |
return wctype (s); |
| 268 |
# endif |
| 269 |
} |
| 270 |
# define IS_CHAR_CLASS(string) is_char_class (string) |
| 271 |
|
| 272 |
# include "fnmatch_loop.c" |
| 273 |
# endif |
| 274 |
|
| 275 |
|
| 276 |
int |
| 277 |
fnmatch (const char *pattern, const char *string, int flags) |
| 278 |
{ |
| 279 |
# if HANDLE_MULTIBYTE |
| 280 |
# define ALLOCA_LIMIT 2000 |
| 281 |
if (__builtin_expect (MB_CUR_MAX, 1) != 1) |
| 282 |
{ |
| 283 |
mbstate_t ps; |
| 284 |
size_t patsize; |
| 285 |
size_t strsize; |
| 286 |
size_t totsize; |
| 287 |
wchar_t *wpattern; |
| 288 |
wchar_t *wstring; |
| 289 |
int res; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
memset (&ps, '\0', sizeof (ps)); |
| 294 |
patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1; |
| 295 |
if (__builtin_expect (patsize != 0, 1)) |
| 296 |
{ |
| 297 |
assert (mbsinit (&ps)); |
| 298 |
strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1; |
| 299 |
if (__builtin_expect (strsize != 0, 1)) |
| 300 |
{ |
| 301 |
assert (mbsinit (&ps)); |
| 302 |
totsize = patsize + strsize; |
| 303 |
if (__builtin_expect (! (patsize <= totsize |
| 304 |
&& totsize <= SIZE_MAX / sizeof (wchar_t)), |
| 305 |
0)) |
| 306 |
{ |
| 307 |
errno = ENOMEM; |
| 308 |
return -1; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
if (__builtin_expect (totsize < ALLOCA_LIMIT, 1)) |
| 313 |
wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t)); |
| 314 |
else |
| 315 |
{ |
| 316 |
wpattern = malloc (totsize * sizeof (wchar_t)); |
| 317 |
if (__builtin_expect (! wpattern, 0)) |
| 318 |
{ |
| 319 |
errno = ENOMEM; |
| 320 |
return -1; |
| 321 |
} |
| 322 |
} |
| 323 |
wstring = wpattern + patsize; |
| 324 |
|
| 325 |
|
| 326 |
mbsrtowcs (wpattern, &pattern, patsize, &ps); |
| 327 |
assert (mbsinit (&ps)); |
| 328 |
mbsrtowcs (wstring, &string, strsize, &ps); |
| 329 |
|
| 330 |
res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1, |
| 331 |
flags & FNM_PERIOD, flags); |
| 332 |
|
| 333 |
if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0)) |
| 334 |
free (wpattern); |
| 335 |
return res; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
# endif |
| 341 |
|
| 342 |
return internal_fnmatch (pattern, string, string + strlen (string), |
| 343 |
flags & FNM_PERIOD, flags); |
| 344 |
} |
| 345 |
|
| 346 |
# ifdef _LIBC |
| 347 |
# undef fnmatch |
| 348 |
versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3); |
| 349 |
# if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3) |
| 350 |
strong_alias (__fnmatch, __fnmatch_old) |
| 351 |
compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0); |
| 352 |
# endif |
| 353 |
libc_hidden_ver (__fnmatch, fnmatch) |
| 354 |
# endif |
| 355 |
|
| 356 |
#endif |