fnmatch.c 11 KB

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