fnmatch.c 14 KB

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