fnmatch.c 13 KB

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