fnmatch_loop.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /* Copyright (C) 1991-1993, 1996-1999, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. This library 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 GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. /* Match STRING against the filename pattern PATTERN, returning zero if
  16. it matches, nonzero if not. */
  17. static int FCT (const CHAR *pattern, const CHAR *string,
  18. int no_leading_period, int flags) internal_function;
  19. static int
  20. internal_function
  21. FCT (pattern, string, no_leading_period, flags)
  22. const CHAR *pattern;
  23. const CHAR *string;
  24. int no_leading_period;
  25. int flags;
  26. {
  27. register const CHAR *p = pattern, *n = string;
  28. register UCHAR c;
  29. #ifdef _LIBC
  30. const UCHAR *collseq = (const UCHAR *)
  31. _NL_CURRENT(LC_COLLATE, CONCAT(_NL_COLLATE_COLLSEQ,SUFFIX));
  32. # ifdef WIDE_CHAR_VERSION
  33. const wint_t *names = (const wint_t *)
  34. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_NAMES);
  35. size_t size = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_HASH_SIZE);
  36. size_t layers = _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_HASH_LAYERS);
  37. # endif
  38. #endif
  39. while ((c = *p++) != L('\0'))
  40. {
  41. c = FOLD (c);
  42. switch (c)
  43. {
  44. case L('?'):
  45. if (*n == L('\0'))
  46. return FNM_NOMATCH;
  47. else if (*n == L('/') && (flags & FNM_FILE_NAME))
  48. return FNM_NOMATCH;
  49. else if (*n == L('.') && no_leading_period
  50. && (n == string
  51. || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
  52. return FNM_NOMATCH;
  53. break;
  54. case L('\\'):
  55. if (!(flags & FNM_NOESCAPE))
  56. {
  57. c = *p++;
  58. if (c == L('\0'))
  59. /* Trailing \ loses. */
  60. return FNM_NOMATCH;
  61. c = FOLD (c);
  62. }
  63. if (FOLD ((UCHAR) *n) != c)
  64. return FNM_NOMATCH;
  65. break;
  66. case L('*'):
  67. if (*n == L('.') && no_leading_period
  68. && (n == string
  69. || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
  70. return FNM_NOMATCH;
  71. for (c = *p++; c == L('?') || c == L('*'); c = *p++)
  72. {
  73. if (*n == L('/') && (flags & FNM_FILE_NAME))
  74. /* A slash does not match a wildcard under FNM_FILE_NAME. */
  75. return FNM_NOMATCH;
  76. else if (c == L('?'))
  77. {
  78. /* A ? needs to match one character. */
  79. if (*n == L('\0'))
  80. /* There isn't another character; no match. */
  81. return FNM_NOMATCH;
  82. else
  83. /* One character of the string is consumed in matching
  84. this ? wildcard, so *??? won't match if there are
  85. less than three characters. */
  86. ++n;
  87. }
  88. }
  89. if (c == L('\0'))
  90. /* The wildcard(s) is/are the last element of the pattern.
  91. If the name is a file name and contains another slash
  92. this does mean it cannot match. If the FNM_LEADING_DIR
  93. flag is set and exactly one slash is following, we have
  94. a match. */
  95. {
  96. int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
  97. if (flags & FNM_FILE_NAME)
  98. {
  99. const CHAR *slashp = STRCHR (n, L('/'));
  100. if (flags & FNM_LEADING_DIR)
  101. {
  102. if (slashp != NULL
  103. && STRCHR (slashp + 1, L('/')) == NULL)
  104. result = 0;
  105. }
  106. else
  107. {
  108. if (slashp == NULL)
  109. result = 0;
  110. }
  111. }
  112. return result;
  113. }
  114. else
  115. {
  116. const CHAR *endp;
  117. endp = STRCHRNUL (n, (flags & FNM_FILE_NAME) ? L('/') : L('\0'));
  118. if (c == L('['))
  119. {
  120. int flags2 = ((flags & FNM_FILE_NAME)
  121. ? flags : (flags & ~FNM_PERIOD));
  122. for (--p; n < endp; ++n)
  123. if (FCT (p, n, (no_leading_period
  124. && (n == string
  125. || (n[-1] == L('/')
  126. && (flags & FNM_FILE_NAME)))),
  127. flags2) == 0)
  128. return 0;
  129. }
  130. else if (c == L('/') && (flags & FNM_FILE_NAME))
  131. {
  132. while (*n != L('\0') && *n != L('/'))
  133. ++n;
  134. if (*n == L('/')
  135. && (FCT (p, n + 1, flags & FNM_PERIOD, flags) == 0))
  136. return 0;
  137. }
  138. else
  139. {
  140. int flags2 = ((flags & FNM_FILE_NAME)
  141. ? flags : (flags & ~FNM_PERIOD));
  142. if (c == L('\\') && !(flags & FNM_NOESCAPE))
  143. c = *p;
  144. c = FOLD (c);
  145. for (--p; n < endp; ++n)
  146. if (FOLD ((UCHAR) *n) == c
  147. && (FCT (p, n, (no_leading_period
  148. && (n == string
  149. || (n[-1] == L('/')
  150. && (flags & FNM_FILE_NAME)))),
  151. flags2) == 0))
  152. return 0;
  153. }
  154. }
  155. /* If we come here no match is possible with the wildcard. */
  156. return FNM_NOMATCH;
  157. case L('['):
  158. {
  159. /* Nonzero if the sense of the character class is inverted. */
  160. static int posixly_correct;
  161. register int not;
  162. CHAR cold;
  163. if (posixly_correct == 0)
  164. posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
  165. if (*n == L('\0'))
  166. return FNM_NOMATCH;
  167. if (*n == L('.') && no_leading_period
  168. && (n == string
  169. || (n[-1] == L('/') && (flags & FNM_FILE_NAME))))
  170. return FNM_NOMATCH;
  171. if (*n == L('/') && (flags & FNM_FILE_NAME))
  172. /* `/' cannot be matched. */
  173. return FNM_NOMATCH;
  174. not = (*p == L('!') || (posixly_correct < 0 && *p == L('^')));
  175. if (not)
  176. ++p;
  177. c = *p++;
  178. for (;;)
  179. {
  180. UCHAR fn = FOLD ((UCHAR) *n);
  181. if (!(flags & FNM_NOESCAPE) && c == L('\\'))
  182. {
  183. if (*p == L('\0'))
  184. return FNM_NOMATCH;
  185. c = FOLD ((UCHAR) *p);
  186. ++p;
  187. if (c == fn)
  188. goto matched;
  189. }
  190. else if (c == L('[') && *p == L(':'))
  191. {
  192. /* Leave room for the null. */
  193. CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
  194. size_t c1 = 0;
  195. #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
  196. wctype_t wt;
  197. #endif
  198. const CHAR *startp = p;
  199. for (;;)
  200. {
  201. if (c1 == CHAR_CLASS_MAX_LENGTH)
  202. /* The name is too long and therefore the pattern
  203. is ill-formed. */
  204. return FNM_NOMATCH;
  205. c = *++p;
  206. if (c == L(':') && p[1] == L(']'))
  207. {
  208. p += 2;
  209. break;
  210. }
  211. if (c < L('a') || c >= L('z'))
  212. {
  213. /* This cannot possibly be a character class name.
  214. Match it as a normal range. */
  215. p = startp;
  216. c = L('[');
  217. goto normal_bracket;
  218. }
  219. str[c1++] = c;
  220. }
  221. str[c1] = L('\0');
  222. #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
  223. wt = IS_CHAR_CLASS (str);
  224. if (wt == 0)
  225. /* Invalid character class name. */
  226. return FNM_NOMATCH;
  227. if (ISWCTYPE (BTOWC ((UCHAR) *n), wt))
  228. goto matched;
  229. #else
  230. if ((STREQ (str, L("alnum")) && ISALNUM ((UCHAR) *n))
  231. || (STREQ (str, L("alpha")) && ISALPHA ((UCHAR) *n))
  232. || (STREQ (str, L("blank")) && ISBLANK ((UCHAR) *n))
  233. || (STREQ (str, L("cntrl")) && ISCNTRL ((UCHAR) *n))
  234. || (STREQ (str, L("digit")) && ISDIGIT ((UCHAR) *n))
  235. || (STREQ (str, L("graph")) && ISGRAPH ((UCHAR) *n))
  236. || (STREQ (str, L("lower")) && ISLOWER ((UCHAR) *n))
  237. || (STREQ (str, L("print")) && ISPRINT ((UCHAR) *n))
  238. || (STREQ (str, L("punct")) && ISPUNCT ((UCHAR) *n))
  239. || (STREQ (str, L("space")) && ISSPACE ((UCHAR) *n))
  240. || (STREQ (str, L("upper")) && ISUPPER ((UCHAR) *n))
  241. || (STREQ (str, L("xdigit")) && ISXDIGIT ((UCHAR) *n)))
  242. goto matched;
  243. #endif
  244. }
  245. else if (c == L('\0'))
  246. /* [ (unterminated) loses. */
  247. return FNM_NOMATCH;
  248. else
  249. {
  250. c = FOLD (c);
  251. normal_bracket:
  252. if (c == fn)
  253. goto matched;
  254. cold = c;
  255. c = *p++;
  256. if (c == L('-') && *p != L(']'))
  257. {
  258. #if _LIBC
  259. /* We have to find the collation sequence
  260. value for C. Collation sequence is nothing
  261. we can regularly access. The sequence
  262. value is defined by the order in which the
  263. definitions of the collation values for the
  264. various characters appear in the source
  265. file. A strange concept, nowhere
  266. documented. */
  267. int32_t fseqidx;
  268. int32_t lseqidx;
  269. UCHAR cend = *p++;
  270. # ifdef WIDE_CHAR_VERSION
  271. size_t cnt;
  272. # endif
  273. if (!(flags & FNM_NOESCAPE) && cend == L('\\'))
  274. cend = *p++;
  275. if (cend == L('\0'))
  276. return FNM_NOMATCH;
  277. # ifdef WIDE_CHAR_VERSION
  278. /* Search in the `names' array for the characters. */
  279. fseqidx = fn % size;
  280. cnt = 0;
  281. while (names[fseqidx] != fn)
  282. {
  283. if (++cnt == layers)
  284. /* XXX We don't know anything about
  285. the character we are supposed to
  286. match. This means we are failing. */
  287. goto range_not_matched;
  288. fseqidx += size;
  289. }
  290. lseqidx = cold % size;
  291. cnt = 0;
  292. while (names[lseqidx] != cold)
  293. {
  294. if (++cnt == layers)
  295. {
  296. lseqidx = -1;
  297. break;
  298. }
  299. lseqidx += size;
  300. }
  301. # else
  302. fseqidx = fn;
  303. lseqidx = cold;
  304. # endif
  305. /* XXX It is not entirely clear to me how to handle
  306. characters which are not mentioned in the
  307. collation specification. */
  308. if (
  309. # ifdef WIDE_CHAR_VERSION
  310. lseqidx == -1 ||
  311. # endif
  312. collseq[lseqidx] <= collseq[fseqidx])
  313. {
  314. /* We have to look at the upper bound. */
  315. int32_t hseqidx;
  316. cend = FOLD (cend);
  317. # ifdef WIDE_CHAR_VERSION
  318. hseqidx = cend % size;
  319. cnt = 0;
  320. while (names[hseqidx] != cend)
  321. {
  322. if (++cnt == layers)
  323. {
  324. /* Hum, no information about the upper
  325. bound. The matching succeeds if the
  326. lower bound is matched exactly. */
  327. if (lseqidx == -1 || cold != fn)
  328. goto range_not_matched;
  329. goto matched;
  330. }
  331. }
  332. # else
  333. hseqidx = cend;
  334. # endif
  335. if (
  336. # ifdef WIDE_CHAR_VERSION
  337. (lseqidx == -1
  338. && collseq[fseqidx] == collseq[hseqidx]) ||
  339. # endif
  340. collseq[fseqidx] <= collseq[hseqidx])
  341. goto matched;
  342. }
  343. # ifdef WIDE_CHAR_VERSION
  344. range_not_matched:
  345. # endif
  346. #else
  347. /* We use a boring value comparison of the character
  348. values. This is better than comparing using
  349. `strcoll' since the latter would have surprising
  350. and sometimes fatal consequences. */
  351. UCHAR cend = *p++;
  352. if (!(flags & FNM_NOESCAPE) && cend == L('\\'))
  353. cend = *p++;
  354. if (cend == L('\0'))
  355. return FNM_NOMATCH;
  356. /* It is a range. */
  357. if (cold <= fc && fc <= c)
  358. goto matched;
  359. #endif
  360. c = *p++;
  361. }
  362. }
  363. if (c == L(']'))
  364. break;
  365. }
  366. if (!not)
  367. return FNM_NOMATCH;
  368. break;
  369. matched:
  370. /* Skip the rest of the [...] that already matched. */
  371. while (c != L(']'))
  372. {
  373. if (c == L('\0'))
  374. /* [... (unterminated) loses. */
  375. return FNM_NOMATCH;
  376. c = *p++;
  377. if (!(flags & FNM_NOESCAPE) && c == L('\\'))
  378. {
  379. if (*p == L('\0'))
  380. return FNM_NOMATCH;
  381. /* XXX 1003.2d11 is unclear if this is right. */
  382. ++p;
  383. }
  384. else if (c == L('[') && *p == L(':'))
  385. {
  386. do
  387. if (*++p == L('\0'))
  388. return FNM_NOMATCH;
  389. while (*p != L(':') || p[1] == L(']'));
  390. p += 2;
  391. c = *p;
  392. }
  393. }
  394. if (not)
  395. return FNM_NOMATCH;
  396. }
  397. break;
  398. default:
  399. if (c != FOLD ((UCHAR) *n))
  400. return FNM_NOMATCH;
  401. }
  402. ++n;
  403. }
  404. if (*n == '\0')
  405. return 0;
  406. if ((flags & FNM_LEADING_DIR) && *n == L('/'))
  407. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  408. return 0;
  409. return FNM_NOMATCH;
  410. }
  411. #undef FOLD
  412. #undef CHAR
  413. #undef UCHAR
  414. #undef FCT
  415. #undef STRCHR
  416. #undef STRCHRNUL
  417. #undef STRCOLL
  418. #undef L
  419. #undef BTOWC
  420. #undef SUFFIX