fnmatch.c 15 KB

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