fnmatch.c 14 KB

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