fnmatch_loop.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. /* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2003,2004,2005
  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, see
  14. <http://www.gnu.org/licenses/>. */
  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. const CHAR *string_end, int no_leading_period, int flags)
  19. internal_function;
  20. static int EXT (INT opt, const CHAR *pattern, const CHAR *string,
  21. const CHAR *string_end, int no_leading_period, int flags)
  22. internal_function;
  23. static const CHAR *END (const CHAR *patternp) internal_function;
  24. static int
  25. internal_function
  26. FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end,
  27. int no_leading_period, int flags)
  28. {
  29. register const CHAR *p = pattern, *n = string;
  30. register UCHAR c;
  31. #ifdef _LIBC
  32. # if WIDE_CHAR_VERSION
  33. const char *collseq = (const char *)
  34. _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC);
  35. # else
  36. const UCHAR *collseq = (const UCHAR *)
  37. _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQMB);
  38. # endif
  39. #endif
  40. while ((c = *p++) != L('\0'))
  41. {
  42. int new_no_leading_period = 0;
  43. c = FOLD (c);
  44. switch (c)
  45. {
  46. case L('?'):
  47. if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
  48. {
  49. int res;
  50. res = EXT (c, p, n, string_end, no_leading_period,
  51. flags);
  52. if (res != -1)
  53. return res;
  54. }
  55. if (n == string_end)
  56. return FNM_NOMATCH;
  57. else if (*n == L('/') && (flags & FNM_FILE_NAME))
  58. return FNM_NOMATCH;
  59. else if (*n == L('.') && no_leading_period)
  60. return FNM_NOMATCH;
  61. break;
  62. case L('\\'):
  63. if (!(flags & FNM_NOESCAPE))
  64. {
  65. c = *p++;
  66. if (c == L('\0'))
  67. /* Trailing \ loses. */
  68. return FNM_NOMATCH;
  69. c = FOLD (c);
  70. }
  71. if (n == string_end || FOLD ((UCHAR) *n) != c)
  72. return FNM_NOMATCH;
  73. break;
  74. case L('*'):
  75. if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
  76. {
  77. int res;
  78. res = EXT (c, p, n, string_end, no_leading_period,
  79. flags);
  80. if (res != -1)
  81. return res;
  82. }
  83. if (n != string_end && *n == L('.') && no_leading_period)
  84. return FNM_NOMATCH;
  85. for (c = *p++; c == L('?') || c == L('*'); c = *p++)
  86. {
  87. if (*p == L('(') && (flags & FNM_EXTMATCH) != 0)
  88. {
  89. const CHAR *endp = END (p);
  90. if (endp != p)
  91. {
  92. /* This is a pattern. Skip over it. */
  93. p = endp;
  94. continue;
  95. }
  96. }
  97. if (c == L('?'))
  98. {
  99. /* A ? needs to match one character. */
  100. if (n == string_end)
  101. /* There isn't another character; no match. */
  102. return FNM_NOMATCH;
  103. else if (*n == L('/')
  104. && __builtin_expect (flags & FNM_FILE_NAME, 0))
  105. /* A slash does not match a wildcard under
  106. FNM_FILE_NAME. */
  107. return FNM_NOMATCH;
  108. else
  109. /* One character of the string is consumed in matching
  110. this ? wildcard, so *??? won't match if there are
  111. less than three characters. */
  112. ++n;
  113. }
  114. }
  115. if (c == L('\0'))
  116. /* The wildcard(s) is/are the last element of the pattern.
  117. If the name is a file name and contains another slash
  118. this means it cannot match, unless the FNM_LEADING_DIR
  119. flag is set. */
  120. {
  121. int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
  122. if (flags & FNM_FILE_NAME)
  123. {
  124. if (flags & FNM_LEADING_DIR)
  125. result = 0;
  126. else
  127. {
  128. if (MEMCHR (n, L('/'), string_end - n) == NULL)
  129. result = 0;
  130. }
  131. }
  132. return result;
  133. }
  134. else
  135. {
  136. const CHAR *endp;
  137. endp = MEMCHR (n, (flags & FNM_FILE_NAME) ? L('/') : L('\0'),
  138. string_end - n);
  139. if (endp == NULL)
  140. endp = string_end;
  141. if (c == L('[')
  142. || (__builtin_expect (flags & FNM_EXTMATCH, 0) != 0
  143. && (c == L('@') || c == L('+') || c == L('!'))
  144. && *p == L('(')))
  145. {
  146. int flags2 = ((flags & FNM_FILE_NAME)
  147. ? flags : (flags & ~FNM_PERIOD));
  148. int no_leading_period2 = no_leading_period;
  149. for (--p; n < endp; ++n, no_leading_period2 = 0)
  150. if (FCT (p, n, string_end, no_leading_period2, flags2)
  151. == 0)
  152. return 0;
  153. }
  154. else if (c == L('/') && (flags & FNM_FILE_NAME))
  155. {
  156. while (n < string_end && *n != L('/'))
  157. ++n;
  158. if (n < string_end && *n == L('/')
  159. && (FCT (p, n + 1, string_end, flags & FNM_PERIOD, flags)
  160. == 0))
  161. return 0;
  162. }
  163. else
  164. {
  165. int flags2 = ((flags & FNM_FILE_NAME)
  166. ? flags : (flags & ~FNM_PERIOD));
  167. int no_leading_period2 = no_leading_period;
  168. if (c == L('\\') && !(flags & FNM_NOESCAPE))
  169. c = *p;
  170. c = FOLD (c);
  171. for (--p; n < endp; ++n, no_leading_period2 = 0)
  172. if (FOLD ((UCHAR) *n) == c
  173. && (FCT (p, n, string_end, no_leading_period2, flags2)
  174. == 0))
  175. return 0;
  176. }
  177. }
  178. /* If we come here no match is possible with the wildcard. */
  179. return FNM_NOMATCH;
  180. case L('['):
  181. {
  182. /* Nonzero if the sense of the character class is inverted. */
  183. register int not;
  184. CHAR cold;
  185. UCHAR fn;
  186. if (posixly_correct == 0)
  187. posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
  188. if (n == string_end)
  189. return FNM_NOMATCH;
  190. if (*n == L('.') && no_leading_period)
  191. return FNM_NOMATCH;
  192. if (*n == L('/') && (flags & FNM_FILE_NAME))
  193. /* `/' cannot be matched. */
  194. return FNM_NOMATCH;
  195. not = (*p == L('!') || (posixly_correct < 0 && *p == L('^')));
  196. if (not)
  197. ++p;
  198. fn = FOLD ((UCHAR) *n);
  199. c = *p++;
  200. for (;;)
  201. {
  202. if (!(flags & FNM_NOESCAPE) && c == L('\\'))
  203. {
  204. if (*p == L('\0'))
  205. return FNM_NOMATCH;
  206. c = FOLD ((UCHAR) *p);
  207. ++p;
  208. goto normal_bracket;
  209. }
  210. else if (c == L('[') && *p == L(':'))
  211. {
  212. /* Leave room for the null. */
  213. CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
  214. size_t c1 = 0;
  215. #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
  216. wctype_t wt;
  217. #endif
  218. const CHAR *startp = p;
  219. for (;;)
  220. {
  221. if (c1 == CHAR_CLASS_MAX_LENGTH)
  222. /* The name is too long and therefore the pattern
  223. is ill-formed. */
  224. return FNM_NOMATCH;
  225. c = *++p;
  226. if (c == L(':') && p[1] == L(']'))
  227. {
  228. p += 2;
  229. break;
  230. }
  231. if (c < L('a') || c >= L('z'))
  232. {
  233. /* This cannot possibly be a character class name.
  234. Match it as a normal range. */
  235. p = startp;
  236. c = L('[');
  237. goto normal_bracket;
  238. }
  239. str[c1++] = c;
  240. }
  241. str[c1] = L('\0');
  242. #if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
  243. wt = IS_CHAR_CLASS (str);
  244. if (wt == 0)
  245. /* Invalid character class name. */
  246. return FNM_NOMATCH;
  247. # if defined _LIBC && ! WIDE_CHAR_VERSION
  248. /* The following code is glibc specific but does
  249. there a good job in speeding up the code since
  250. we can avoid the btowc() call. */
  251. if (_ISCTYPE ((UCHAR) *n, wt))
  252. goto matched;
  253. # else
  254. if (ISWCTYPE (BTOWC ((UCHAR) *n), wt))
  255. goto matched;
  256. # endif
  257. #else
  258. if ((STREQ (str, L("alnum")) && ISALNUM ((UCHAR) *n))
  259. || (STREQ (str, L("alpha")) && ISALPHA ((UCHAR) *n))
  260. || (STREQ (str, L("blank")) && ISBLANK ((UCHAR) *n))
  261. || (STREQ (str, L("cntrl")) && ISCNTRL ((UCHAR) *n))
  262. || (STREQ (str, L("digit")) && ISDIGIT ((UCHAR) *n))
  263. || (STREQ (str, L("graph")) && ISGRAPH ((UCHAR) *n))
  264. || (STREQ (str, L("lower")) && ISLOWER ((UCHAR) *n))
  265. || (STREQ (str, L("print")) && ISPRINT ((UCHAR) *n))
  266. || (STREQ (str, L("punct")) && ISPUNCT ((UCHAR) *n))
  267. || (STREQ (str, L("space")) && ISSPACE ((UCHAR) *n))
  268. || (STREQ (str, L("upper")) && ISUPPER ((UCHAR) *n))
  269. || (STREQ (str, L("xdigit")) && ISXDIGIT ((UCHAR) *n)))
  270. goto matched;
  271. #endif
  272. c = *p++;
  273. }
  274. #ifdef _LIBC
  275. else if (c == L('[') && *p == L('='))
  276. {
  277. UCHAR str[1];
  278. uint32_t nrules =
  279. _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  280. const CHAR *startp = p;
  281. c = *++p;
  282. if (c == L('\0'))
  283. {
  284. p = startp;
  285. c = L('[');
  286. goto normal_bracket;
  287. }
  288. str[0] = c;
  289. c = *++p;
  290. if (c != L('=') || p[1] != L(']'))
  291. {
  292. p = startp;
  293. c = L('[');
  294. goto normal_bracket;
  295. }
  296. p += 2;
  297. if (nrules == 0)
  298. {
  299. if ((UCHAR) *n == str[0])
  300. goto matched;
  301. }
  302. else
  303. {
  304. const int32_t *table;
  305. # if WIDE_CHAR_VERSION
  306. const int32_t *weights;
  307. const int32_t *extra;
  308. # else
  309. const unsigned char *weights;
  310. const unsigned char *extra;
  311. # endif
  312. const int32_t *indirect;
  313. int32_t idx;
  314. const UCHAR *cp = (const UCHAR *) str;
  315. /* This #include defines a local function! */
  316. # if WIDE_CHAR_VERSION
  317. # include <locale/weightwc.h>
  318. # else
  319. # include <locale/weight.h>
  320. # endif
  321. # if WIDE_CHAR_VERSION
  322. table = (const int32_t *)
  323. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEWC);
  324. weights = (const int32_t *)
  325. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTWC);
  326. extra = (const int32_t *)
  327. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAWC);
  328. indirect = (const int32_t *)
  329. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTWC);
  330. # else
  331. table = (const int32_t *)
  332. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_TABLEMB);
  333. weights = (const unsigned char *)
  334. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_WEIGHTMB);
  335. extra = (const unsigned char *)
  336. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_EXTRAMB);
  337. indirect = (const int32_t *)
  338. _NL_CURRENT (LC_COLLATE, _NL_COLLATE_INDIRECTMB);
  339. # endif
  340. idx = findidx (&cp);
  341. if (idx != 0)
  342. {
  343. /* We found a table entry. Now see whether the
  344. character we are currently at has the same
  345. equivalance class value. */
  346. int len = weights[idx];
  347. int32_t idx2;
  348. const UCHAR *np = (const UCHAR *) n;
  349. idx2 = findidx (&np);
  350. if (idx2 != 0 && len == weights[idx2])
  351. {
  352. int cnt = 0;
  353. while (cnt < len
  354. && (weights[idx + 1 + cnt]
  355. == weights[idx2 + 1 + cnt]))
  356. ++cnt;
  357. if (cnt == len)
  358. goto matched;
  359. }
  360. }
  361. }
  362. c = *p++;
  363. }
  364. #endif
  365. else if (c == L('\0'))
  366. /* [ (unterminated) loses. */
  367. return FNM_NOMATCH;
  368. else
  369. {
  370. int is_range = 0;
  371. #ifdef _LIBC
  372. int is_seqval = 0;
  373. if (c == L('[') && *p == L('.'))
  374. {
  375. uint32_t nrules =
  376. _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  377. const CHAR *startp = p;
  378. size_t c1 = 0;
  379. while (1)
  380. {
  381. c = *++p;
  382. if (c == L('.') && p[1] == L(']'))
  383. {
  384. p += 2;
  385. break;
  386. }
  387. if (c == '\0')
  388. return FNM_NOMATCH;
  389. ++c1;
  390. }
  391. /* We have to handling the symbols differently in
  392. ranges since then the collation sequence is
  393. important. */
  394. is_range = *p == L('-') && p[1] != L('\0');
  395. if (nrules == 0)
  396. {
  397. /* There are no names defined in the collation
  398. data. Therefore we only accept the trivial
  399. names consisting of the character itself. */
  400. if (c1 != 1)
  401. return FNM_NOMATCH;
  402. if (!is_range && *n == startp[1])
  403. goto matched;
  404. cold = startp[1];
  405. c = *p++;
  406. }
  407. else
  408. {
  409. int32_t table_size;
  410. const int32_t *symb_table;
  411. # ifdef WIDE_CHAR_VERSION
  412. char str[c1];
  413. unsigned int strcnt;
  414. # else
  415. # define str (startp + 1)
  416. # endif
  417. const unsigned char *extra;
  418. int32_t idx;
  419. int32_t elem;
  420. int32_t second;
  421. int32_t hash;
  422. # ifdef WIDE_CHAR_VERSION
  423. /* We have to convert the name to a single-byte
  424. string. This is possible since the names
  425. consist of ASCII characters and the internal
  426. representation is UCS4. */
  427. for (strcnt = 0; strcnt < c1; ++strcnt)
  428. str[strcnt] = startp[1 + strcnt];
  429. #endif
  430. table_size =
  431. _NL_CURRENT_WORD (LC_COLLATE,
  432. _NL_COLLATE_SYMB_HASH_SIZEMB);
  433. symb_table = (const int32_t *)
  434. _NL_CURRENT (LC_COLLATE,
  435. _NL_COLLATE_SYMB_TABLEMB);
  436. extra = (const unsigned char *)
  437. _NL_CURRENT (LC_COLLATE,
  438. _NL_COLLATE_SYMB_EXTRAMB);
  439. /* Locate the character in the hashing table. */
  440. hash = elem_hash (str, c1);
  441. idx = 0;
  442. elem = hash % table_size;
  443. if (symb_table[2 * elem] != 0)
  444. {
  445. second = hash % (table_size - 2) + 1;
  446. do
  447. {
  448. /* First compare the hashing value. */
  449. if (symb_table[2 * elem] == hash
  450. && (c1
  451. == extra[symb_table[2 * elem + 1]])
  452. && memcmp (str,
  453. &extra[symb_table[2 * elem
  454. + 1]
  455. + 1], c1) == 0)
  456. {
  457. /* Yep, this is the entry. */
  458. idx = symb_table[2 * elem + 1];
  459. idx += 1 + extra[idx];
  460. break;
  461. }
  462. /* Next entry. */
  463. elem += second;
  464. }
  465. while (symb_table[2 * elem] != 0);
  466. }
  467. if (symb_table[2 * elem] != 0)
  468. {
  469. /* Compare the byte sequence but only if
  470. this is not part of a range. */
  471. # ifdef WIDE_CHAR_VERSION
  472. int32_t *wextra;
  473. idx += 1 + extra[idx];
  474. /* Adjust for the alignment. */
  475. idx = (idx + 3) & ~3;
  476. wextra = (int32_t *) &extra[idx + 4];
  477. # endif
  478. if (! is_range)
  479. {
  480. # ifdef WIDE_CHAR_VERSION
  481. for (c1 = 0;
  482. (int32_t) c1 < wextra[idx];
  483. ++c1)
  484. if (n[c1] != wextra[1 + c1])
  485. break;
  486. if ((int32_t) c1 == wextra[idx])
  487. goto matched;
  488. # else
  489. for (c1 = 0; c1 < extra[idx]; ++c1)
  490. if (n[c1] != extra[1 + c1])
  491. break;
  492. if (c1 == extra[idx])
  493. goto matched;
  494. # endif
  495. }
  496. /* Get the collation sequence value. */
  497. is_seqval = 1;
  498. # ifdef WIDE_CHAR_VERSION
  499. cold = wextra[1 + wextra[idx]];
  500. # else
  501. /* Adjust for the alignment. */
  502. idx += 1 + extra[idx];
  503. idx = (idx + 3) & ~4;
  504. cold = *((int32_t *) &extra[idx]);
  505. # endif
  506. c = *p++;
  507. }
  508. else if (c1 == 1)
  509. {
  510. /* No valid character. Match it as a
  511. single byte. */
  512. if (!is_range && *n == str[0])
  513. goto matched;
  514. cold = str[0];
  515. c = *p++;
  516. }
  517. else
  518. return FNM_NOMATCH;
  519. }
  520. }
  521. else
  522. # undef str
  523. #endif
  524. {
  525. c = FOLD (c);
  526. normal_bracket:
  527. /* We have to handling the symbols differently in
  528. ranges since then the collation sequence is
  529. important. */
  530. is_range = (*p == L('-') && p[1] != L('\0')
  531. && p[1] != L(']'));
  532. if (!is_range && c == fn)
  533. goto matched;
  534. /* This is needed if we goto normal_bracket; from
  535. outside of is_seqval's scope. */
  536. #ifndef __UCLIBC__ /* this should be probably ifdef _LIBC*/
  537. is_seqval = 0;
  538. #endif
  539. cold = c;
  540. c = *p++;
  541. }
  542. if (c == L('-') && *p != L(']'))
  543. {
  544. #if _LIBC
  545. /* We have to find the collation sequence
  546. value for C. Collation sequence is nothing
  547. we can regularly access. The sequence
  548. value is defined by the order in which the
  549. definitions of the collation values for the
  550. various characters appear in the source
  551. file. A strange concept, nowhere
  552. documented. */
  553. uint32_t fcollseq;
  554. uint32_t lcollseq;
  555. UCHAR cend = *p++;
  556. # ifdef WIDE_CHAR_VERSION
  557. /* Search in the `names' array for the characters. */
  558. fcollseq = __collseq_table_lookup (collseq, fn);
  559. if (fcollseq == ~((uint32_t) 0))
  560. /* XXX We don't know anything about the character
  561. we are supposed to match. This means we are
  562. failing. */
  563. goto range_not_matched;
  564. if (is_seqval)
  565. lcollseq = cold;
  566. else
  567. lcollseq = __collseq_table_lookup (collseq, cold);
  568. # else
  569. fcollseq = collseq[fn];
  570. lcollseq = is_seqval ? cold : collseq[(UCHAR) cold];
  571. # endif
  572. is_seqval = 0;
  573. if (cend == L('[') && *p == L('.'))
  574. {
  575. uint32_t nrules =
  576. _NL_CURRENT_WORD (LC_COLLATE,
  577. _NL_COLLATE_NRULES);
  578. const CHAR *startp = p;
  579. size_t c1 = 0;
  580. while (1)
  581. {
  582. c = *++p;
  583. if (c == L('.') && p[1] == L(']'))
  584. {
  585. p += 2;
  586. break;
  587. }
  588. if (c == '\0')
  589. return FNM_NOMATCH;
  590. ++c1;
  591. }
  592. if (nrules == 0)
  593. {
  594. /* There are no names defined in the
  595. collation data. Therefore we only
  596. accept the trivial names consisting
  597. of the character itself. */
  598. if (c1 != 1)
  599. return FNM_NOMATCH;
  600. cend = startp[1];
  601. }
  602. else
  603. {
  604. int32_t table_size;
  605. const int32_t *symb_table;
  606. # ifdef WIDE_CHAR_VERSION
  607. char str[c1];
  608. unsigned int strcnt;
  609. # else
  610. # define str (startp + 1)
  611. # endif
  612. const unsigned char *extra;
  613. int32_t idx;
  614. int32_t elem;
  615. int32_t second;
  616. int32_t hash;
  617. # ifdef WIDE_CHAR_VERSION
  618. /* We have to convert the name to a single-byte
  619. string. This is possible since the names
  620. consist of ASCII characters and the internal
  621. representation is UCS4. */
  622. for (strcnt = 0; strcnt < c1; ++strcnt)
  623. str[strcnt] = startp[1 + strcnt];
  624. # endif
  625. table_size =
  626. _NL_CURRENT_WORD (LC_COLLATE,
  627. _NL_COLLATE_SYMB_HASH_SIZEMB);
  628. symb_table = (const int32_t *)
  629. _NL_CURRENT (LC_COLLATE,
  630. _NL_COLLATE_SYMB_TABLEMB);
  631. extra = (const unsigned char *)
  632. _NL_CURRENT (LC_COLLATE,
  633. _NL_COLLATE_SYMB_EXTRAMB);
  634. /* Locate the character in the hashing
  635. table. */
  636. hash = elem_hash (str, c1);
  637. idx = 0;
  638. elem = hash % table_size;
  639. if (symb_table[2 * elem] != 0)
  640. {
  641. second = hash % (table_size - 2) + 1;
  642. do
  643. {
  644. /* First compare the hashing value. */
  645. if (symb_table[2 * elem] == hash
  646. && (c1
  647. == extra[symb_table[2 * elem + 1]])
  648. && memcmp (str,
  649. &extra[symb_table[2 * elem + 1]
  650. + 1], c1) == 0)
  651. {
  652. /* Yep, this is the entry. */
  653. idx = symb_table[2 * elem + 1];
  654. idx += 1 + extra[idx];
  655. break;
  656. }
  657. /* Next entry. */
  658. elem += second;
  659. }
  660. while (symb_table[2 * elem] != 0);
  661. }
  662. if (symb_table[2 * elem] != 0)
  663. {
  664. /* Compare the byte sequence but only if
  665. this is not part of a range. */
  666. # ifdef WIDE_CHAR_VERSION
  667. int32_t *wextra;
  668. idx += 1 + extra[idx];
  669. /* Adjust for the alignment. */
  670. idx = (idx + 3) & ~4;
  671. wextra = (int32_t *) &extra[idx + 4];
  672. # endif
  673. /* Get the collation sequence value. */
  674. is_seqval = 1;
  675. # ifdef WIDE_CHAR_VERSION
  676. cend = wextra[1 + wextra[idx]];
  677. # else
  678. /* Adjust for the alignment. */
  679. idx += 1 + extra[idx];
  680. idx = (idx + 3) & ~4;
  681. cend = *((int32_t *) &extra[idx]);
  682. # endif
  683. }
  684. else if (symb_table[2 * elem] != 0 && c1 == 1)
  685. {
  686. cend = str[0];
  687. c = *p++;
  688. }
  689. else
  690. return FNM_NOMATCH;
  691. }
  692. # undef str
  693. }
  694. else
  695. {
  696. if (!(flags & FNM_NOESCAPE) && cend == L('\\'))
  697. cend = *p++;
  698. if (cend == L('\0'))
  699. return FNM_NOMATCH;
  700. cend = FOLD (cend);
  701. }
  702. /* XXX It is not entirely clear to me how to handle
  703. characters which are not mentioned in the
  704. collation specification. */
  705. if (
  706. # ifdef WIDE_CHAR_VERSION
  707. lcollseq == 0xffffffff ||
  708. # endif
  709. lcollseq <= fcollseq)
  710. {
  711. /* We have to look at the upper bound. */
  712. uint32_t hcollseq;
  713. if (is_seqval)
  714. hcollseq = cend;
  715. else
  716. {
  717. # ifdef WIDE_CHAR_VERSION
  718. hcollseq =
  719. __collseq_table_lookup (collseq, cend);
  720. if (hcollseq == ~((uint32_t) 0))
  721. {
  722. /* Hum, no information about the upper
  723. bound. The matching succeeds if the
  724. lower bound is matched exactly. */
  725. if (lcollseq != fcollseq)
  726. goto range_not_matched;
  727. goto matched;
  728. }
  729. # else
  730. hcollseq = collseq[cend];
  731. # endif
  732. }
  733. if (lcollseq <= hcollseq && fcollseq <= hcollseq)
  734. goto matched;
  735. }
  736. # ifdef WIDE_CHAR_VERSION
  737. range_not_matched:
  738. # endif
  739. #else
  740. /* We use a boring value comparison of the character
  741. values. This is better than comparing using
  742. `strcoll' since the latter would have surprising
  743. and sometimes fatal consequences. */
  744. UCHAR cend = *p++;
  745. if (!(flags & FNM_NOESCAPE) && cend == L('\\'))
  746. cend = *p++;
  747. if (cend == L('\0'))
  748. return FNM_NOMATCH;
  749. /* It is a range. */
  750. if (cold <= fn && fn <= cend)
  751. goto matched;
  752. #endif
  753. c = *p++;
  754. }
  755. }
  756. if (c == L(']'))
  757. break;
  758. }
  759. if (!not)
  760. return FNM_NOMATCH;
  761. break;
  762. matched:
  763. /* Skip the rest of the [...] that already matched. */
  764. do
  765. {
  766. ignore_next:
  767. c = *p++;
  768. if (c == L('\0'))
  769. /* [... (unterminated) loses. */
  770. return FNM_NOMATCH;
  771. if (!(flags & FNM_NOESCAPE) && c == L('\\'))
  772. {
  773. if (*p == L('\0'))
  774. return FNM_NOMATCH;
  775. /* XXX 1003.2d11 is unclear if this is right. */
  776. ++p;
  777. }
  778. else if (c == L('[') && *p == L(':'))
  779. {
  780. int c1 = 0;
  781. const CHAR *startp = p;
  782. while (1)
  783. {
  784. c = *++p;
  785. if (++c1 == CHAR_CLASS_MAX_LENGTH)
  786. return FNM_NOMATCH;
  787. if (*p == L(':') && p[1] == L(']'))
  788. break;
  789. if (c < L('a') || c >= L('z'))
  790. {
  791. p = startp;
  792. goto ignore_next;
  793. }
  794. }
  795. p += 2;
  796. c = *p++;
  797. }
  798. else if (c == L('[') && *p == L('='))
  799. {
  800. c = *++p;
  801. if (c == L('\0'))
  802. return FNM_NOMATCH;
  803. c = *++p;
  804. if (c != L('=') || p[1] != L(']'))
  805. return FNM_NOMATCH;
  806. p += 2;
  807. c = *p++;
  808. }
  809. else if (c == L('[') && *p == L('.'))
  810. {
  811. ++p;
  812. while (1)
  813. {
  814. c = *++p;
  815. if (c == '\0')
  816. return FNM_NOMATCH;
  817. if (*p == L('.') && p[1] == L(']'))
  818. break;
  819. }
  820. p += 2;
  821. c = *p++;
  822. }
  823. }
  824. while (c != L(']'));
  825. if (not)
  826. return FNM_NOMATCH;
  827. }
  828. break;
  829. case L('+'):
  830. case L('@'):
  831. case L('!'):
  832. if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
  833. {
  834. int res;
  835. res = EXT (c, p, n, string_end, no_leading_period, flags);
  836. if (res != -1)
  837. return res;
  838. }
  839. goto normal_match;
  840. case L('/'):
  841. if (NO_LEADING_PERIOD (flags))
  842. {
  843. if (n == string_end || c != (UCHAR) *n)
  844. return FNM_NOMATCH;
  845. new_no_leading_period = 1;
  846. break;
  847. }
  848. /* FALLTHROUGH */
  849. default:
  850. normal_match:
  851. if (n == string_end || c != FOLD ((UCHAR) *n))
  852. return FNM_NOMATCH;
  853. }
  854. no_leading_period = new_no_leading_period;
  855. ++n;
  856. }
  857. if (n == string_end)
  858. return 0;
  859. if ((flags & FNM_LEADING_DIR) && n != string_end && *n == L('/'))
  860. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  861. return 0;
  862. return FNM_NOMATCH;
  863. }
  864. static const CHAR *
  865. internal_function
  866. END (const CHAR *pattern)
  867. {
  868. const CHAR *p = pattern;
  869. while (1)
  870. if (*++p == L('\0'))
  871. /* This is an invalid pattern. */
  872. return pattern;
  873. else if (*p == L('['))
  874. {
  875. /* Handle brackets special. */
  876. if (posixly_correct == 0)
  877. posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
  878. /* Skip the not sign. We have to recognize it because of a possibly
  879. following ']'. */
  880. if (*++p == L('!') || (posixly_correct < 0 && *p == L('^')))
  881. ++p;
  882. /* A leading ']' is recognized as such. */
  883. if (*p == L(']'))
  884. ++p;
  885. /* Skip over all characters of the list. */
  886. while (*p != L(']'))
  887. if (*p++ == L('\0'))
  888. /* This is no valid pattern. */
  889. return pattern;
  890. }
  891. else if ((*p == L('?') || *p == L('*') || *p == L('+') || *p == L('@')
  892. || *p == L('!')) && p[1] == L('('))
  893. p = END (p + 1);
  894. else if (*p == L(')'))
  895. break;
  896. return p + 1;
  897. }
  898. static int
  899. internal_function
  900. EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end,
  901. int no_leading_period, int flags)
  902. {
  903. const CHAR *startp;
  904. int level;
  905. struct patternlist
  906. {
  907. struct patternlist *next;
  908. CHAR str[0];
  909. } *list = NULL;
  910. struct patternlist **lastp = &list;
  911. size_t pattern_len = STRLEN (pattern);
  912. const CHAR *p;
  913. const CHAR *rs;
  914. /* Parse the pattern. Store the individual parts in the list. */
  915. level = 0;
  916. for (startp = p = pattern + 1; level >= 0; ++p)
  917. if (*p == L('\0'))
  918. /* This is an invalid pattern. */
  919. return -1;
  920. else if (*p == L('['))
  921. {
  922. /* Handle brackets special. */
  923. if (posixly_correct == 0)
  924. posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
  925. /* Skip the not sign. We have to recognize it because of a possibly
  926. following ']'. */
  927. if (*++p == L('!') || (posixly_correct < 0 && *p == L('^')))
  928. ++p;
  929. /* A leading ']' is recognized as such. */
  930. if (*p == L(']'))
  931. ++p;
  932. /* Skip over all characters of the list. */
  933. while (*p != L(']'))
  934. if (*p++ == L('\0'))
  935. /* This is no valid pattern. */
  936. return -1;
  937. }
  938. else if ((*p == L('?') || *p == L('*') || *p == L('+') || *p == L('@')
  939. || *p == L('!')) && p[1] == L('('))
  940. /* Remember the nesting level. */
  941. ++level;
  942. else if (*p == L(')'))
  943. {
  944. if (level-- == 0)
  945. {
  946. /* This means we found the end of the pattern. */
  947. #define NEW_PATTERN \
  948. struct patternlist *newp; \
  949. \
  950. if (opt == L('?') || opt == L('@')) \
  951. newp = alloca (sizeof (struct patternlist) \
  952. + (pattern_len * sizeof (CHAR))); \
  953. else \
  954. newp = alloca (sizeof (struct patternlist) \
  955. + ((p - startp + 1) * sizeof (CHAR))); \
  956. *((CHAR *) MEMPCPY (newp->str, startp, p - startp)) = L('\0'); \
  957. newp->next = NULL; \
  958. *lastp = newp; \
  959. lastp = &newp->next
  960. NEW_PATTERN;
  961. }
  962. }
  963. else if (*p == L('|'))
  964. {
  965. if (level == 0)
  966. {
  967. NEW_PATTERN;
  968. startp = p + 1;
  969. }
  970. }
  971. assert (list != NULL);
  972. assert (p[-1] == L(')'));
  973. #undef NEW_PATTERN
  974. switch (opt)
  975. {
  976. case L('*'):
  977. if (FCT (p, string, string_end, no_leading_period, flags) == 0)
  978. return 0;
  979. /* FALLTHROUGH */
  980. case L('+'):
  981. do
  982. {
  983. for (rs = string; rs <= string_end; ++rs)
  984. /* First match the prefix with the current pattern with the
  985. current pattern. */
  986. if (FCT (list->str, string, rs, no_leading_period,
  987. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0
  988. /* This was successful. Now match the rest with the rest
  989. of the pattern. */
  990. && (FCT (p, rs, string_end,
  991. rs == string
  992. ? no_leading_period
  993. : rs[-1] == '/' && NO_LEADING_PERIOD (flags) ? 1 : 0,
  994. flags & FNM_FILE_NAME
  995. ? flags : flags & ~FNM_PERIOD) == 0
  996. /* This didn't work. Try the whole pattern. */
  997. || (rs != string
  998. && FCT (pattern - 1, rs, string_end,
  999. rs == string
  1000. ? no_leading_period
  1001. : (rs[-1] == '/' && NO_LEADING_PERIOD (flags)
  1002. ? 1 : 0),
  1003. flags & FNM_FILE_NAME
  1004. ? flags : flags & ~FNM_PERIOD) == 0)))
  1005. /* It worked. Signal success. */
  1006. return 0;
  1007. }
  1008. while ((list = list->next) != NULL);
  1009. /* None of the patterns lead to a match. */
  1010. return FNM_NOMATCH;
  1011. case L('?'):
  1012. if (FCT (p, string, string_end, no_leading_period, flags) == 0)
  1013. return 0;
  1014. /* FALLTHROUGH */
  1015. case L('@'):
  1016. do
  1017. /* I cannot believe it but `strcat' is actually acceptable
  1018. here. Match the entire string with the prefix from the
  1019. pattern list and the rest of the pattern following the
  1020. pattern list. */
  1021. if (FCT (STRCAT (list->str, p), string, string_end,
  1022. no_leading_period,
  1023. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
  1024. /* It worked. Signal success. */
  1025. return 0;
  1026. while ((list = list->next) != NULL);
  1027. /* None of the patterns lead to a match. */
  1028. return FNM_NOMATCH;
  1029. case L('!'):
  1030. for (rs = string; rs <= string_end; ++rs)
  1031. {
  1032. struct patternlist *runp;
  1033. for (runp = list; runp != NULL; runp = runp->next)
  1034. if (FCT (runp->str, string, rs, no_leading_period,
  1035. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
  1036. break;
  1037. /* If none of the patterns matched see whether the rest does. */
  1038. if (runp == NULL
  1039. && (FCT (p, rs, string_end,
  1040. rs == string
  1041. ? no_leading_period
  1042. : rs[-1] == '/' && NO_LEADING_PERIOD (flags) ? 1 : 0,
  1043. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD)
  1044. == 0))
  1045. /* This is successful. */
  1046. return 0;
  1047. }
  1048. /* None of the patterns together with the rest of the pattern
  1049. lead to a match. */
  1050. return FNM_NOMATCH;
  1051. default:
  1052. assert (! "Invalid extended matching operator");
  1053. break;
  1054. }
  1055. return -1;
  1056. }
  1057. #undef FOLD
  1058. #undef CHAR
  1059. #undef UCHAR
  1060. #undef INT
  1061. #undef FCT
  1062. #undef EXT
  1063. #undef END
  1064. #undef MEMPCPY
  1065. #undef MEMCHR
  1066. #undef STRCOLL
  1067. #undef STRLEN
  1068. #undef STRCAT
  1069. #undef L
  1070. #undef BTOWC