fnmatch_loop.c 29 KB

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