fnmatch.c 13 KB

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