fnmatch.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003
  2. Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #if HAVE_CONFIG_H
  17. # include <config.h>
  18. #endif
  19. /* Enable GNU extensions in fnmatch.h. */
  20. #ifndef _GNU_SOURCE
  21. # define _GNU_SOURCE 1
  22. #endif
  23. #include <features.h>
  24. #ifdef __UCLIBC__
  25. # undef _LIBC
  26. # define HAVE_STRING_H 1
  27. # define STDC_HEADERS
  28. # define HAVE___STRCHRNUL 1
  29. extern void *__mempcpy (void *__restrict __dest,
  30. __const void *__restrict __src, size_t __n)
  31. __THROW __nonnull ((1, 2)) attribute_hidden;
  32. extern void *__memchr (__const void *__s, int __c, size_t __n)
  33. __THROW __attribute_pure__ __nonnull ((1)) attribute_hidden;
  34. # ifdef __UCLIBC_HAS_WCHAR__
  35. # define HAVE_WCHAR_H 1
  36. # define HAVE_WCTYPE_H 1
  37. # ifdef __UCLIBC_HAS_LOCALE__
  38. # define HAVE_MBSTATE_T 1
  39. # define HAVE_MBSRTOWCS 1
  40. # endif
  41. # endif
  42. #endif
  43. #include <assert.h>
  44. #include <errno.h>
  45. #include <fnmatch.h>
  46. #include <ctype.h>
  47. #if HAVE_STRING_H || defined _LIBC
  48. # include <string.h>
  49. #else
  50. # include <strings.h>
  51. #endif
  52. #if defined STDC_HEADERS || defined _LIBC
  53. # include <stdlib.h>
  54. #endif
  55. /* For platform which support the ISO C amendement 1 functionality we
  56. support user defined character classes. */
  57. #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
  58. /* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>. */
  59. # include <wchar.h>
  60. # include <wctype.h>
  61. # ifdef __UCLIBC__
  62. extern wctype_t __wctype (__const char *__property) __THROW attribute_hidden;
  63. extern int __iswctype (wint_t __wc, wctype_t __desc) __THROW attribute_hidden;
  64. extern wint_t __btowc (int __c) __THROW attribute_hidden;
  65. # ifdef __UCLIBC_HAS_LOCALE__
  66. extern size_t __mbsrtowcs (wchar_t *__restrict __dst,
  67. __const char **__restrict __src, size_t __len,
  68. mbstate_t *__restrict __ps) __THROW attribute_hidden;
  69. extern size_t __wcslen (__const wchar_t *__s) __THROW __attribute_pure__ attribute_hidden;
  70. extern wchar_t *__wmempcpy (wchar_t *__restrict __s1,
  71. __const wchar_t *__restrict __s2, size_t __n)
  72. __THROW attribute_hidden;
  73. extern wchar_t *__wcscat (wchar_t *__restrict __dest,
  74. __const wchar_t *__restrict __src) __THROW attribute_hidden;
  75. extern size_t __strnlen (__const char *__string, size_t __maxlen)
  76. __THROW __attribute_pure__ __nonnull ((1)) attribute_hidden;
  77. extern wchar_t *__wmemchr (__const wchar_t *__s, wchar_t __c, size_t __n)
  78. __THROW __attribute_pure__ attribute_hidden;
  79. extern wint_t __towlower (wint_t __wc) __THROW attribute_hidden;
  80. # endif
  81. # endif
  82. #endif
  83. /* We need some of the locale data (the collation sequence information)
  84. but there is no interface to get this information in general. Therefore
  85. we support a correct implementation only in glibc. */
  86. #if defined _LIBC || defined __UCLIBC__
  87. # ifndef __UCLIBC__
  88. # include "../locale/localeinfo.h"
  89. # include "../locale/elem-hash.h"
  90. # include "../locale/coll-lookup.h"
  91. # include <shlib-compat.h>
  92. # endif
  93. # define CONCAT(a,b) __CONCAT(a,b)
  94. # if defined _LIBC || defined __UCLIBC_HAS_LOCALE__
  95. # define mbsrtowcs __mbsrtowcs
  96. # endif
  97. # define fnmatch __fnmatch
  98. extern int fnmatch (const char *pattern, const char *string, int flags) attribute_hidden;
  99. #endif
  100. /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
  101. #define NO_LEADING_PERIOD(flags) \
  102. ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
  103. /* Comment out all this code if we are using the GNU C Library, and are not
  104. actually compiling the library itself. This code is part of the GNU C
  105. Library, but also included in many other GNU distributions. Compiling
  106. and linking in this code is a waste when using the GNU C library
  107. (especially if it is a shared library). Rather than having every GNU
  108. program understand `configure --with-gnu-libc' and omit the object files,
  109. it is simpler to just do this in the source for each such file. */
  110. #if defined _LIBC || !defined __GNU_LIBRARY__
  111. # if defined STDC_HEADERS || !defined isascii
  112. # define ISASCII(c) 1
  113. # else
  114. # define ISASCII(c) isascii(c)
  115. # endif
  116. # ifdef isblank
  117. # define ISBLANK(c) (ISASCII (c) && isblank (c))
  118. # else
  119. # define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  120. # endif
  121. # ifdef isgraph
  122. # define ISGRAPH(c) (ISASCII (c) && isgraph (c))
  123. # else
  124. # define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
  125. # endif
  126. # define ISPRINT(c) (ISASCII (c) && isprint (c))
  127. # define ISDIGIT(c) (ISASCII (c) && isdigit (c))
  128. # define ISALNUM(c) (ISASCII (c) && isalnum (c))
  129. # define ISALPHA(c) (ISASCII (c) && isalpha (c))
  130. # define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
  131. # define ISLOWER(c) (ISASCII (c) && islower (c))
  132. # define ISPUNCT(c) (ISASCII (c) && ispunct (c))
  133. # define ISSPACE(c) (ISASCII (c) && isspace (c))
  134. # define ISUPPER(c) (ISASCII (c) && isupper (c))
  135. # define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
  136. # define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  137. # if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
  138. /* The GNU C library provides support for user-defined character classes
  139. and the functions from ISO C amendement 1. */
  140. # ifdef CHARCLASS_NAME_MAX
  141. # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
  142. # else
  143. /* This shouldn't happen but some implementation might still have this
  144. problem. Use a reasonable default value. */
  145. # define CHAR_CLASS_MAX_LENGTH 256
  146. # endif
  147. # if defined _LIBC || defined __UCLIBC__
  148. # define IS_CHAR_CLASS(string) __wctype (string)
  149. # else
  150. # define IS_CHAR_CLASS(string) wctype (string)
  151. # endif
  152. # if defined _LIBC || defined __UCLIBC__
  153. # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
  154. # else
  155. # define ISWCTYPE(WC, WT) iswctype (WC, WT)
  156. # endif
  157. # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
  158. /* In this case we are implementing the multibyte character handling. */
  159. # define HANDLE_MULTIBYTE 1
  160. # endif
  161. # else
  162. # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, `xdigit'. */
  163. # define IS_CHAR_CLASS(string) \
  164. (STREQ (string, "alpha") || STREQ (string, "upper") \
  165. || STREQ (string, "lower") || STREQ (string, "digit") \
  166. || STREQ (string, "alnum") || STREQ (string, "xdigit") \
  167. || STREQ (string, "space") || STREQ (string, "print") \
  168. || STREQ (string, "punct") || STREQ (string, "graph") \
  169. || STREQ (string, "cntrl") || STREQ (string, "blank"))
  170. # endif
  171. /* Avoid depending on library functions or files
  172. whose names are inconsistent. */
  173. # if !defined _LIBC && !defined getenv && !defined __UCLIBC__
  174. extern char *getenv ();
  175. # endif
  176. # ifndef errno
  177. extern int errno;
  178. # endif
  179. /* Global variable. */
  180. static int posixly_correct;
  181. /* This function doesn't exist on most systems. */
  182. # if !defined HAVE___STRCHRNUL && !defined _LIBC
  183. static char *
  184. __strchrnul (s, c)
  185. const char *s;
  186. int c;
  187. {
  188. char *result = strchr (s, c);
  189. if (result == NULL)
  190. result = strchr (s, '\0');
  191. return result;
  192. }
  193. # endif
  194. # if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
  195. static wchar_t *
  196. __wcschrnul (s, c)
  197. const wchar_t *s;
  198. wint_t c;
  199. {
  200. wchar_t *result = wcschr (s, c);
  201. if (result == NULL)
  202. result = wcschr (s, '\0');
  203. return result;
  204. }
  205. # endif
  206. # ifndef internal_function
  207. /* Inside GNU libc we mark some function in a special way. In other
  208. environments simply ignore the marking. */
  209. # define internal_function
  210. # endif
  211. /* Note that this evaluates C many times. */
  212. # if defined _LIBC || defined __UCLIBC__
  213. # define FOLD(c) ((flags & FNM_CASEFOLD) ? __tolower (c) : (c))
  214. # else
  215. # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
  216. # endif
  217. # define CHAR char
  218. # define UCHAR unsigned char
  219. # define INT int
  220. # define FCT internal_fnmatch
  221. # define EXT ext_match
  222. # define END end_pattern
  223. # define L(CS) CS
  224. # if defined _LIBC || defined __UCLIBC__
  225. # define BTOWC(C) __btowc (C)
  226. # else
  227. # define BTOWC(C) btowc (C)
  228. # endif
  229. # define STRLEN(S) __strlen (S)
  230. # define STRCAT(D, S) __strcat (D, S)
  231. # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
  232. # define MEMCHR(S, C, N) __memchr (S, C, N)
  233. # define STRCOLL(S1, S2) __strcoll (S1, S2)
  234. # include "fnmatch_loop.c"
  235. # if HANDLE_MULTIBYTE
  236. /* Note that this evaluates C many times. */
  237. # if defined _LIBC || defined __UCLIBC__
  238. # define FOLD(c) ((flags & FNM_CASEFOLD) ? __towlower (c) : (c))
  239. # else
  240. # define FOLD(c) ((flags & FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
  241. # endif
  242. # define CHAR wchar_t
  243. # define UCHAR wint_t
  244. # define INT wint_t
  245. # define FCT internal_fnwmatch
  246. # define EXT ext_wmatch
  247. # define END end_wpattern
  248. # define L(CS) L##CS
  249. # define BTOWC(C) (C)
  250. # define STRLEN(S) __wcslen (S)
  251. # define STRCAT(D, S) __wcscat (D, S)
  252. # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
  253. # define MEMCHR(S, C, N) __wmemchr (S, C, N)
  254. # define STRCOLL(S1, S2) __wcscoll (S1, S2)
  255. # ifndef __UCLIBC__
  256. # define WIDE_CHAR_VERSION 1
  257. # endif
  258. # undef IS_CHAR_CLASS
  259. /* We have to convert the wide character string in a multibyte string. But
  260. we know that the character class names consist of alphanumeric characters
  261. from the portable character set, and since the wide character encoding
  262. for a member of the portable character set is the same code point as
  263. its single-byte encoding, we can use a simplified method to convert the
  264. string to a multibyte character string. */
  265. static wctype_t
  266. is_char_class (const wchar_t *wcs)
  267. {
  268. char s[CHAR_CLASS_MAX_LENGTH + 1];
  269. char *cp = s;
  270. do
  271. {
  272. /* Test for a printable character from the portable character set. */
  273. # if defined _LIBC || defined __UCLIBC__
  274. if (*wcs < 0x20 || *wcs > 0x7e
  275. || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
  276. return (wctype_t) 0;
  277. # else
  278. switch (*wcs)
  279. {
  280. case L' ': case L'!': case L'"': case L'#': case L'%':
  281. case L'&': case L'\'': case L'(': case L')': case L'*':
  282. case L'+': case L',': case L'-': case L'.': case L'/':
  283. case L'0': case L'1': case L'2': case L'3': case L'4':
  284. case L'5': case L'6': case L'7': case L'8': case L'9':
  285. case L':': case L';': case L'<': case L'=': case L'>':
  286. case L'?':
  287. case L'A': case L'B': case L'C': case L'D': case L'E':
  288. case L'F': case L'G': case L'H': case L'I': case L'J':
  289. case L'K': case L'L': case L'M': case L'N': case L'O':
  290. case L'P': case L'Q': case L'R': case L'S': case L'T':
  291. case L'U': case L'V': case L'W': case L'X': case L'Y':
  292. case L'Z':
  293. case L'[': case L'\\': case L']': case L'^': case L'_':
  294. case L'a': case L'b': case L'c': case L'd': case L'e':
  295. case L'f': case L'g': case L'h': case L'i': case L'j':
  296. case L'k': case L'l': case L'm': case L'n': case L'o':
  297. case L'p': case L'q': case L'r': case L's': case L't':
  298. case L'u': case L'v': case L'w': case L'x': case L'y':
  299. case L'z': case L'{': case L'|': case L'}': case L'~':
  300. break;
  301. default:
  302. return (wctype_t) 0;
  303. }
  304. # endif
  305. /* Avoid overrunning the buffer. */
  306. if (cp == s + CHAR_CLASS_MAX_LENGTH)
  307. return (wctype_t) 0;
  308. *cp++ = (char) *wcs++;
  309. }
  310. while (*wcs != L'\0');
  311. *cp = '\0';
  312. # if defined _LIBC || defined __UCLIBC__
  313. return __wctype (s);
  314. # else
  315. return wctype (s);
  316. # endif
  317. }
  318. # define IS_CHAR_CLASS(string) is_char_class (string)
  319. # include "fnmatch_loop.c"
  320. # endif
  321. int attribute_hidden
  322. fnmatch (const char *pattern, const char *string, int flags)
  323. {
  324. # if HANDLE_MULTIBYTE
  325. # ifdef __UCLIBC_HAS_WCHAR__
  326. # undef MB_CUR_MAX
  327. # define MB_CUR_MAX (_stdlib_mb_cur_max_internal ())
  328. extern size_t _stdlib_mb_cur_max_internal (void) __THROW __wur attribute_hidden;
  329. # endif
  330. if (__builtin_expect (MB_CUR_MAX, 1) != 1)
  331. {
  332. mbstate_t ps;
  333. size_t n;
  334. const char *p;
  335. wchar_t *wpattern = NULL;
  336. wchar_t *wstring = NULL;
  337. /* Convert the strings into wide characters. */
  338. __memset (&ps, '\0', sizeof (ps));
  339. p = pattern;
  340. #if defined _LIBC || defined __UCLIBC__
  341. n = __strnlen (pattern, 1024);
  342. #else
  343. n = __strlen (pattern);
  344. #endif
  345. if (__builtin_expect (n < 1024, 1))
  346. {
  347. wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
  348. n = mbsrtowcs (wpattern, &p, n + 1, &ps);
  349. if (__builtin_expect (n == (size_t) -1, 0))
  350. /* Something wrong.
  351. XXX Do we have to set `errno' to something which mbsrtows hasn't
  352. already done? */
  353. return -1;
  354. if (p)
  355. __memset (&ps, '\0', sizeof (ps));
  356. }
  357. if (__builtin_expect (p != NULL, 0))
  358. {
  359. n = mbsrtowcs (NULL, &pattern, 0, &ps);
  360. if (__builtin_expect (n == (size_t) -1, 0))
  361. /* Something wrong.
  362. XXX Do we have to set `errno' to something which mbsrtows hasn't
  363. already done? */
  364. return -1;
  365. wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
  366. assert (mbsinit (&ps));
  367. (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
  368. }
  369. assert (mbsinit (&ps));
  370. #if defined _LIBC || defined __UCLIBC__
  371. n = __strnlen (string, 1024);
  372. #else
  373. n = __strlen (string);
  374. #endif
  375. p = string;
  376. if (__builtin_expect (n < 1024, 1))
  377. {
  378. wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
  379. n = mbsrtowcs (wstring, &p, n + 1, &ps);
  380. if (__builtin_expect (n == (size_t) -1, 0))
  381. /* Something wrong.
  382. XXX Do we have to set `errno' to something which mbsrtows hasn't
  383. already done? */
  384. return -1;
  385. if (p)
  386. __memset (&ps, '\0', sizeof (ps));
  387. }
  388. if (__builtin_expect (p != NULL, 0))
  389. {
  390. n = mbsrtowcs (NULL, &string, 0, &ps);
  391. if (__builtin_expect (n == (size_t) -1, 0))
  392. /* Something wrong.
  393. XXX Do we have to set `errno' to something which mbsrtows hasn't
  394. already done? */
  395. return -1;
  396. wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t));
  397. assert (mbsinit (&ps));
  398. (void) mbsrtowcs (wstring, &string, n + 1, &ps);
  399. }
  400. return internal_fnwmatch (wpattern, wstring, wstring + n,
  401. flags & FNM_PERIOD, flags);
  402. }
  403. # endif /* mbstate_t and mbsrtowcs or _LIBC. */
  404. return internal_fnmatch (pattern, string, string + __strlen (string),
  405. flags & FNM_PERIOD, flags);
  406. }
  407. # if defined _LIBC || defined __UCLIBC__
  408. # undef fnmatch
  409. # ifndef __UCLIBC__
  410. versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
  411. # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
  412. strong_alias (__fnmatch, __fnmatch_old)
  413. compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
  414. # endif
  415. libc_hidden_ver (__fnmatch, fnmatch)
  416. # else
  417. strong_alias(__fnmatch,fnmatch)
  418. # endif
  419. # endif
  420. #endif /* _LIBC or not __GNU_LIBRARY__. */