fnmatch.c 13 KB

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