fnmatch.c 13 KB

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