fnmatch.c 14 KB

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