fnmatch.c 14 KB

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