fnmatch.c 14 KB

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