fnmatch.c 14 KB

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