fnmatch_loop.c 45 KB

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