fnmatch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* Copyright (C) 1991-1993, 1996-2007, 2009-2013 Free Software Foundation, Inc.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 3, or (at your option)
  5. any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  12. #ifndef _LIBC
  13. # include <config.h>
  14. #endif
  15. /* Enable GNU extensions in fnmatch.h. */
  16. #ifndef _GNU_SOURCE
  17. # define _GNU_SOURCE 1
  18. #endif
  19. #if ! defined __builtin_expect && __GNUC__ < 3
  20. # define __builtin_expect(expr, expected) (expr)
  21. #endif
  22. #include <fnmatch.h>
  23. #include <alloca.h>
  24. #include <assert.h>
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <stddef.h>
  28. #include <stdbool.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #define WIDE_CHAR_SUPPORT \
  32. (HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \
  33. && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
  34. /* For platform which support the ISO C amendment 1 functionality we
  35. support user defined character classes. */
  36. #if defined _LIBC || WIDE_CHAR_SUPPORT
  37. # include <wctype.h>
  38. # include <wchar.h>
  39. #endif
  40. /* We need some of the locale data (the collation sequence information)
  41. but there is no interface to get this information in general. Therefore
  42. we support a correct implementation only in glibc. */
  43. #ifdef _LIBC
  44. # include "../locale/localeinfo.h"
  45. # include "../locale/elem-hash.h"
  46. # include "../locale/coll-lookup.h"
  47. # include <shlib-compat.h>
  48. # define CONCAT(a,b) __CONCAT(a,b)
  49. # define mbsrtowcs __mbsrtowcs
  50. # define fnmatch __fnmatch
  51. extern int fnmatch (const char *pattern, const char *string, int flags);
  52. #endif
  53. #ifndef SIZE_MAX
  54. # define SIZE_MAX ((size_t) -1)
  55. #endif
  56. /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set. */
  57. #define NO_LEADING_PERIOD(flags) \
  58. ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
  59. /* Comment out all this code if we are using the GNU C Library, and are not
  60. actually compiling the library itself, and have not detected a bug
  61. in the library. This code is part of the GNU C
  62. Library, but also included in many other GNU distributions. Compiling
  63. and linking in this code is a waste when using the GNU C library
  64. (especially if it is a shared library). Rather than having every GNU
  65. program understand 'configure --with-gnu-libc' and omit the object files,
  66. it is simpler to just do this in the source for each such file. */
  67. #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
  68. # if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
  69. # define isblank(c) ((c) == ' ' || (c) == '\t')
  70. # endif
  71. # define STREQ(s1, s2) (strcmp (s1, s2) == 0)
  72. # if defined _LIBC || WIDE_CHAR_SUPPORT
  73. /* The GNU C library provides support for user-defined character classes
  74. and the functions from ISO C amendment 1. */
  75. # ifdef CHARCLASS_NAME_MAX
  76. # define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
  77. # else
  78. /* This shouldn't happen but some implementation might still have this
  79. problem. Use a reasonable default value. */
  80. # define CHAR_CLASS_MAX_LENGTH 256
  81. # endif
  82. # ifdef _LIBC
  83. # define IS_CHAR_CLASS(string) __wctype (string)
  84. # else
  85. # define IS_CHAR_CLASS(string) wctype (string)
  86. # endif
  87. # ifdef _LIBC
  88. # define ISWCTYPE(WC, WT) __iswctype (WC, WT)
  89. # else
  90. # define ISWCTYPE(WC, WT) iswctype (WC, WT)
  91. # endif
  92. # if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
  93. /* In this case we are implementing the multibyte character handling. */
  94. # define HANDLE_MULTIBYTE 1
  95. # endif
  96. # else
  97. # define CHAR_CLASS_MAX_LENGTH 6 /* Namely, 'xdigit'. */
  98. # define IS_CHAR_CLASS(string) \
  99. (STREQ (string, "alpha") || STREQ (string, "upper") \
  100. || STREQ (string, "lower") || STREQ (string, "digit") \
  101. || STREQ (string, "alnum") || STREQ (string, "xdigit") \
  102. || STREQ (string, "space") || STREQ (string, "print") \
  103. || STREQ (string, "punct") || STREQ (string, "graph") \
  104. || STREQ (string, "cntrl") || STREQ (string, "blank"))
  105. # endif
  106. /* Avoid depending on library functions or files
  107. whose names are inconsistent. */
  108. /* Global variable. */
  109. static int posixly_correct;
  110. # ifndef internal_function
  111. /* Inside GNU libc we mark some function in a special way. In other
  112. environments simply ignore the marking. */
  113. # define internal_function
  114. # endif
  115. /* Note that this evaluates C many times. */
  116. # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
  117. # define CHAR char
  118. # define UCHAR unsigned char
  119. # define INT int
  120. # define FCT internal_fnmatch
  121. # define EXT ext_match
  122. # define END end_pattern
  123. # define L_(CS) CS
  124. # ifdef _LIBC
  125. # define BTOWC(C) __btowc (C)
  126. # else
  127. # define BTOWC(C) btowc (C)
  128. # endif
  129. # define STRLEN(S) strlen (S)
  130. # define STRCAT(D, S) strcat (D, S)
  131. # ifdef _LIBC
  132. # define MEMPCPY(D, S, N) __mempcpy (D, S, N)
  133. # else
  134. # if HAVE_MEMPCPY
  135. # define MEMPCPY(D, S, N) mempcpy (D, S, N)
  136. # else
  137. # define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
  138. # endif
  139. # endif
  140. # define MEMCHR(S, C, N) memchr (S, C, N)
  141. # include "fnmatch_loop.c"
  142. # if HANDLE_MULTIBYTE
  143. # define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
  144. # define CHAR wchar_t
  145. # define UCHAR wint_t
  146. # define INT wint_t
  147. # define FCT internal_fnwmatch
  148. # define EXT ext_wmatch
  149. # define END end_wpattern
  150. # define L_(CS) L##CS
  151. # define BTOWC(C) (C)
  152. # ifdef _LIBC
  153. # define STRLEN(S) __wcslen (S)
  154. # define STRCAT(D, S) __wcscat (D, S)
  155. # define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
  156. # else
  157. # define STRLEN(S) wcslen (S)
  158. # define STRCAT(D, S) wcscat (D, S)
  159. # if HAVE_WMEMPCPY
  160. # define MEMPCPY(D, S, N) wmempcpy (D, S, N)
  161. # else
  162. # define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
  163. # endif
  164. # endif
  165. # define MEMCHR(S, C, N) wmemchr (S, C, N)
  166. # define WIDE_CHAR_VERSION 1
  167. # undef IS_CHAR_CLASS
  168. /* We have to convert the wide character string in a multibyte string. But
  169. we know that the character class names consist of alphanumeric characters
  170. from the portable character set, and since the wide character encoding
  171. for a member of the portable character set is the same code point as
  172. its single-byte encoding, we can use a simplified method to convert the
  173. string to a multibyte character string. */
  174. static wctype_t
  175. is_char_class (const wchar_t *wcs)
  176. {
  177. char s[CHAR_CLASS_MAX_LENGTH + 1];
  178. char *cp = s;
  179. do
  180. {
  181. /* Test for a printable character from the portable character set. */
  182. # ifdef _LIBC
  183. if (*wcs < 0x20 || *wcs > 0x7e
  184. || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
  185. return (wctype_t) 0;
  186. # else
  187. switch (*wcs)
  188. {
  189. case L' ': case L'!': case L'"': case L'#': case L'%':
  190. case L'&': case L'\'': case L'(': case L')': case L'*':
  191. case L'+': case L',': case L'-': case L'.': case L'/':
  192. case L'0': case L'1': case L'2': case L'3': case L'4':
  193. case L'5': case L'6': case L'7': case L'8': case L'9':
  194. case L':': case L';': case L'<': case L'=': case L'>':
  195. case L'?':
  196. case L'A': case L'B': case L'C': case L'D': case L'E':
  197. case L'F': case L'G': case L'H': case L'I': case L'J':
  198. case L'K': case L'L': case L'M': case L'N': case L'O':
  199. case L'P': case L'Q': case L'R': case L'S': case L'T':
  200. case L'U': case L'V': case L'W': case L'X': case L'Y':
  201. case L'Z':
  202. case L'[': case L'\\': case L']': case L'^': case L'_':
  203. case L'a': case L'b': case L'c': case L'd': case L'e':
  204. case L'f': case L'g': case L'h': case L'i': case L'j':
  205. case L'k': case L'l': case L'm': case L'n': case L'o':
  206. case L'p': case L'q': case L'r': case L's': case L't':
  207. case L'u': case L'v': case L'w': case L'x': case L'y':
  208. case L'z': case L'{': case L'|': case L'}': case L'~':
  209. break;
  210. default:
  211. return (wctype_t) 0;
  212. }
  213. # endif
  214. /* Avoid overrunning the buffer. */
  215. if (cp == s + CHAR_CLASS_MAX_LENGTH)
  216. return (wctype_t) 0;
  217. *cp++ = (char) *wcs++;
  218. }
  219. while (*wcs != L'\0');
  220. *cp = '\0';
  221. # ifdef _LIBC
  222. return __wctype (s);
  223. # else
  224. return wctype (s);
  225. # endif
  226. }
  227. # define IS_CHAR_CLASS(string) is_char_class (string)
  228. # include "fnmatch_loop.c"
  229. # endif
  230. int
  231. fnmatch (const char *pattern, const char *string, int flags)
  232. {
  233. # if HANDLE_MULTIBYTE
  234. # define ALLOCA_LIMIT 2000
  235. if (__builtin_expect (MB_CUR_MAX, 1) != 1)
  236. {
  237. mbstate_t ps;
  238. size_t patsize;
  239. size_t strsize;
  240. size_t totsize;
  241. wchar_t *wpattern;
  242. wchar_t *wstring;
  243. int res;
  244. /* Calculate the size needed to convert the strings to
  245. wide characters. */
  246. memset (&ps, '\0', sizeof (ps));
  247. patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
  248. if (__builtin_expect (patsize != 0, 1))
  249. {
  250. assert (mbsinit (&ps));
  251. strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
  252. if (__builtin_expect (strsize != 0, 1))
  253. {
  254. assert (mbsinit (&ps));
  255. totsize = patsize + strsize;
  256. if (__builtin_expect (! (patsize <= totsize
  257. && totsize <= SIZE_MAX / sizeof (wchar_t)),
  258. 0))
  259. {
  260. errno = ENOMEM;
  261. return -1;
  262. }
  263. /* Allocate room for the wide characters. */
  264. if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
  265. wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
  266. else
  267. {
  268. wpattern = malloc (totsize * sizeof (wchar_t));
  269. if (__builtin_expect (! wpattern, 0))
  270. {
  271. errno = ENOMEM;
  272. return -1;
  273. }
  274. }
  275. wstring = wpattern + patsize;
  276. /* Convert the strings into wide characters. */
  277. mbsrtowcs (wpattern, &pattern, patsize, &ps);
  278. assert (mbsinit (&ps));
  279. mbsrtowcs (wstring, &string, strsize, &ps);
  280. res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
  281. flags & FNM_PERIOD, flags);
  282. if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
  283. free (wpattern);
  284. return res;
  285. }
  286. }
  287. }
  288. # endif /* HANDLE_MULTIBYTE */
  289. return internal_fnmatch (pattern, string, string + strlen (string),
  290. flags & FNM_PERIOD, flags);
  291. }
  292. # ifdef _LIBC
  293. # undef fnmatch
  294. versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
  295. # if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
  296. strong_alias (__fnmatch, __fnmatch_old)
  297. compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
  298. # endif
  299. libc_hidden_ver (__fnmatch, fnmatch)
  300. # endif
  301. #endif /* _LIBC or not __GNU_LIBRARY__. */