fnmatch_loop.c 29 KB

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