fnmatch.c 13 KB

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