fnmatch_loop.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  1. /* Copyright (C) 1991-1993, 1996-2006, 2009-2013 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>. */
  13. /* Match STRING against the file name pattern PATTERN, returning zero if
  14. it matches, nonzero if not. */
  15. static int EXT (INT opt, const CHAR *pattern, const CHAR *string,
  16. const CHAR *string_end, bool no_leading_period, int flags)
  17. internal_function;
  18. static const CHAR *END (const CHAR *patternp) internal_function;
  19. static int
  20. internal_function
  21. FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end,
  22. bool no_leading_period, int flags)
  23. {
  24. register const CHAR *p = pattern, *n = string;
  25. register UCHAR c;
  26. #ifdef _LIBC
  27. # if WIDE_CHAR_VERSION
  28. const char *collseq = (const char *)
  29. _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQWC);
  30. # else
  31. const UCHAR *collseq = (const UCHAR *)
  32. _NL_CURRENT(LC_COLLATE, _NL_COLLATE_COLLSEQMB);
  33. # endif
  34. #endif
  35. while ((c = *p++) != L_('\0'))
  36. {
  37. bool new_no_leading_period = false;
  38. c = FOLD (c);
  39. switch (c)
  40. {
  41. case L_('?'):
  42. if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
  43. {
  44. int res;
  45. res = EXT (c, p, n, string_end, no_leading_period,
  46. flags);
  47. if (res != -1)
  48. return res;
  49. }
  50. if (n == string_end)
  51. return FNM_NOMATCH;
  52. else if (*n == L_('/') && (flags & FNM_FILE_NAME))
  53. return FNM_NOMATCH;
  54. else if (*n == L_('.') && no_leading_period)
  55. return FNM_NOMATCH;
  56. break;
  57. case L_('\\'):
  58. if (!(flags & FNM_NOESCAPE))
  59. {
  60. c = *p++;
  61. if (c == L_('\0'))
  62. /* Trailing \ loses. */
  63. return FNM_NOMATCH;
  64. c = FOLD (c);
  65. }
  66. if (n == string_end || FOLD ((UCHAR) *n) != c)
  67. return FNM_NOMATCH;
  68. break;
  69. case L_('*'):
  70. if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
  71. {
  72. int res;
  73. res = EXT (c, p, n, string_end, no_leading_period,
  74. flags);
  75. if (res != -1)
  76. return res;
  77. }
  78. if (n != string_end && *n == L_('.') && no_leading_period)
  79. return FNM_NOMATCH;
  80. for (c = *p++; c == L_('?') || c == L_('*'); c = *p++)
  81. {
  82. if (*p == L_('(') && (flags & FNM_EXTMATCH) != 0)
  83. {
  84. const CHAR *endp = END (p);
  85. if (endp != p)
  86. {
  87. /* This is a pattern. Skip over it. */
  88. p = endp;
  89. continue;
  90. }
  91. }
  92. if (c == L_('?'))
  93. {
  94. /* A ? needs to match one character. */
  95. if (n == string_end)
  96. /* There isn't another character; no match. */
  97. return FNM_NOMATCH;
  98. else if (*n == L_('/')
  99. && __builtin_expect (flags & FNM_FILE_NAME, 0))
  100. /* A slash does not match a wildcard under
  101. FNM_FILE_NAME. */
  102. return FNM_NOMATCH;
  103. else
  104. /* One character of the string is consumed in matching
  105. this ? wildcard, so *??? won't match if there are
  106. less than three characters. */
  107. ++n;
  108. }
  109. }
  110. if (c == L_('\0'))
  111. /* The wildcard(s) is/are the last element of the pattern.
  112. If the name is a file name and contains another slash
  113. this means it cannot match, unless the FNM_LEADING_DIR
  114. flag is set. */
  115. {
  116. int result = (flags & FNM_FILE_NAME) == 0 ? 0 : FNM_NOMATCH;
  117. if (flags & FNM_FILE_NAME)
  118. {
  119. if (flags & FNM_LEADING_DIR)
  120. result = 0;
  121. else
  122. {
  123. if (MEMCHR (n, L_('/'), string_end - n) == NULL)
  124. result = 0;
  125. }
  126. }
  127. return result;
  128. }
  129. else
  130. {
  131. const CHAR *endp;
  132. endp = MEMCHR (n, (flags & FNM_FILE_NAME) ? L_('/') : L_('\0'),
  133. string_end - n);
  134. if (endp == NULL)
  135. endp = string_end;
  136. if (c == L_('[')
  137. || (__builtin_expect (flags & FNM_EXTMATCH, 0) != 0
  138. && (c == L_('@') || c == L_('+') || c == L_('!'))
  139. && *p == L_('(')))
  140. {
  141. int flags2 = ((flags & FNM_FILE_NAME)
  142. ? flags : (flags & ~FNM_PERIOD));
  143. bool no_leading_period2 = no_leading_period;
  144. for (--p; n < endp; ++n, no_leading_period2 = false)
  145. if (FCT (p, n, string_end, no_leading_period2, flags2)
  146. == 0)
  147. return 0;
  148. }
  149. else if (c == L_('/') && (flags & FNM_FILE_NAME))
  150. {
  151. while (n < string_end && *n != L_('/'))
  152. ++n;
  153. if (n < string_end && *n == L_('/')
  154. && (FCT (p, n + 1, string_end, flags & FNM_PERIOD, flags)
  155. == 0))
  156. return 0;
  157. }
  158. else
  159. {
  160. int flags2 = ((flags & FNM_FILE_NAME)
  161. ? flags : (flags & ~FNM_PERIOD));
  162. int no_leading_period2 = no_leading_period;
  163. if (c == L_('\\') && !(flags & FNM_NOESCAPE))
  164. c = *p;
  165. c = FOLD (c);
  166. for (--p; n < endp; ++n, no_leading_period2 = false)
  167. if (FOLD ((UCHAR) *n) == c
  168. && (FCT (p, n, string_end, no_leading_period2, flags2)
  169. == 0))
  170. return 0;
  171. }
  172. }
  173. /* If we come here no match is possible with the wildcard. */
  174. return FNM_NOMATCH;
  175. case L_('['):
  176. {
  177. /* Nonzero if the sense of the character class is inverted. */
  178. const CHAR *p_init = p;
  179. const CHAR *n_init = n;
  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. equivalence 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. {
  368. /* [ unterminated, treat as normal character. */
  369. p = p_init;
  370. n = n_init;
  371. c = L_('[');
  372. goto normal_match;
  373. }
  374. else
  375. {
  376. bool is_range = false;
  377. #ifdef _LIBC
  378. bool is_seqval = false;
  379. if (c == L_('[') && *p == L_('.'))
  380. {
  381. uint32_t nrules =
  382. _NL_CURRENT_WORD (LC_COLLATE, _NL_COLLATE_NRULES);
  383. const CHAR *startp = p;
  384. size_t c1 = 0;
  385. while (1)
  386. {
  387. c = *++p;
  388. if (c == L_('.') && p[1] == L_(']'))
  389. {
  390. p += 2;
  391. break;
  392. }
  393. if (c == '\0')
  394. return FNM_NOMATCH;
  395. ++c1;
  396. }
  397. /* We have to handling the symbols differently in
  398. ranges since then the collation sequence is
  399. important. */
  400. is_range = *p == L_('-') && p[1] != L_('\0');
  401. if (nrules == 0)
  402. {
  403. /* There are no names defined in the collation
  404. data. Therefore we only accept the trivial
  405. names consisting of the character itself. */
  406. if (c1 != 1)
  407. return FNM_NOMATCH;
  408. if (!is_range && *n == startp[1])
  409. goto matched;
  410. cold = startp[1];
  411. c = *p++;
  412. }
  413. else
  414. {
  415. int32_t table_size;
  416. const int32_t *symb_table;
  417. # ifdef WIDE_CHAR_VERSION
  418. char str[c1];
  419. size_t strcnt;
  420. # else
  421. # define str (startp + 1)
  422. # endif
  423. const unsigned char *extra;
  424. int32_t idx;
  425. int32_t elem;
  426. int32_t second;
  427. int32_t hash;
  428. # ifdef WIDE_CHAR_VERSION
  429. /* We have to convert the name to a single-byte
  430. string. This is possible since the names
  431. consist of ASCII characters and the internal
  432. representation is UCS4. */
  433. for (strcnt = 0; strcnt < c1; ++strcnt)
  434. str[strcnt] = startp[1 + strcnt];
  435. # endif
  436. table_size =
  437. _NL_CURRENT_WORD (LC_COLLATE,
  438. _NL_COLLATE_SYMB_HASH_SIZEMB);
  439. symb_table = (const int32_t *)
  440. _NL_CURRENT (LC_COLLATE,
  441. _NL_COLLATE_SYMB_TABLEMB);
  442. extra = (const unsigned char *)
  443. _NL_CURRENT (LC_COLLATE,
  444. _NL_COLLATE_SYMB_EXTRAMB);
  445. /* Locate the character in the hashing table. */
  446. hash = elem_hash (str, c1);
  447. idx = 0;
  448. elem = hash % table_size;
  449. if (symb_table[2 * elem] != 0)
  450. {
  451. second = hash % (table_size - 2) + 1;
  452. do
  453. {
  454. /* First compare the hashing value. */
  455. if (symb_table[2 * elem] == hash
  456. && (c1
  457. == extra[symb_table[2 * elem + 1]])
  458. && memcmp (str,
  459. &extra[symb_table[2 * elem
  460. + 1]
  461. + 1], c1) == 0)
  462. {
  463. /* Yep, this is the entry. */
  464. idx = symb_table[2 * elem + 1];
  465. idx += 1 + extra[idx];
  466. break;
  467. }
  468. /* Next entry. */
  469. elem += second;
  470. }
  471. while (symb_table[2 * elem] != 0);
  472. }
  473. if (symb_table[2 * elem] != 0)
  474. {
  475. /* Compare the byte sequence but only if
  476. this is not part of a range. */
  477. # ifdef WIDE_CHAR_VERSION
  478. int32_t *wextra;
  479. idx += 1 + extra[idx];
  480. /* Adjust for the alignment. */
  481. idx = (idx + 3) & ~3;
  482. wextra = (int32_t *) &extra[idx + 4];
  483. # endif
  484. if (! is_range)
  485. {
  486. # ifdef WIDE_CHAR_VERSION
  487. for (c1 = 0;
  488. (int32_t) c1 < wextra[idx];
  489. ++c1)
  490. if (n[c1] != wextra[1 + c1])
  491. break;
  492. if ((int32_t) c1 == wextra[idx])
  493. goto matched;
  494. # else
  495. for (c1 = 0; c1 < extra[idx]; ++c1)
  496. if (n[c1] != extra[1 + c1])
  497. break;
  498. if (c1 == extra[idx])
  499. goto matched;
  500. # endif
  501. }
  502. /* Get the collation sequence value. */
  503. is_seqval = true;
  504. # ifdef WIDE_CHAR_VERSION
  505. cold = wextra[1 + wextra[idx]];
  506. # else
  507. /* Adjust for the alignment. */
  508. idx += 1 + extra[idx];
  509. idx = (idx + 3) & ~4;
  510. cold = *((int32_t *) &extra[idx]);
  511. # endif
  512. c = *p++;
  513. }
  514. else if (c1 == 1)
  515. {
  516. /* No valid character. Match it as a
  517. single byte. */
  518. if (!is_range && *n == str[0])
  519. goto matched;
  520. cold = str[0];
  521. c = *p++;
  522. }
  523. else
  524. return FNM_NOMATCH;
  525. }
  526. }
  527. else
  528. # undef str
  529. #endif
  530. {
  531. c = FOLD (c);
  532. normal_bracket:
  533. /* We have to handling the symbols differently in
  534. ranges since then the collation sequence is
  535. important. */
  536. is_range = (*p == L_('-') && p[1] != L_('\0')
  537. && p[1] != L_(']'));
  538. if (!is_range && c == fn)
  539. goto matched;
  540. #if _LIBC
  541. /* This is needed if we goto normal_bracket; from
  542. outside of is_seqval's scope. */
  543. is_seqval = false;
  544. #endif
  545. cold = c;
  546. c = *p++;
  547. }
  548. if (c == L_('-') && *p != L_(']'))
  549. {
  550. #if _LIBC
  551. /* We have to find the collation sequence
  552. value for C. Collation sequence is nothing
  553. we can regularly access. The sequence
  554. value is defined by the order in which the
  555. definitions of the collation values for the
  556. various characters appear in the source
  557. file. A strange concept, nowhere
  558. documented. */
  559. uint32_t fcollseq;
  560. uint32_t lcollseq;
  561. UCHAR cend = *p++;
  562. # ifdef WIDE_CHAR_VERSION
  563. /* Search in the 'names' array for the characters. */
  564. fcollseq = __collseq_table_lookup (collseq, fn);
  565. if (fcollseq == ~((uint32_t) 0))
  566. /* XXX We don't know anything about the character
  567. we are supposed to match. This means we are
  568. failing. */
  569. goto range_not_matched;
  570. if (is_seqval)
  571. lcollseq = cold;
  572. else
  573. lcollseq = __collseq_table_lookup (collseq, cold);
  574. # else
  575. fcollseq = collseq[fn];
  576. lcollseq = is_seqval ? cold : collseq[(UCHAR) cold];
  577. # endif
  578. is_seqval = false;
  579. if (cend == L_('[') && *p == L_('.'))
  580. {
  581. uint32_t nrules =
  582. _NL_CURRENT_WORD (LC_COLLATE,
  583. _NL_COLLATE_NRULES);
  584. const CHAR *startp = p;
  585. size_t c1 = 0;
  586. while (1)
  587. {
  588. c = *++p;
  589. if (c == L_('.') && p[1] == L_(']'))
  590. {
  591. p += 2;
  592. break;
  593. }
  594. if (c == '\0')
  595. return FNM_NOMATCH;
  596. ++c1;
  597. }
  598. if (nrules == 0)
  599. {
  600. /* There are no names defined in the
  601. collation data. Therefore we only
  602. accept the trivial names consisting
  603. of the character itself. */
  604. if (c1 != 1)
  605. return FNM_NOMATCH;
  606. cend = startp[1];
  607. }
  608. else
  609. {
  610. int32_t table_size;
  611. const int32_t *symb_table;
  612. # ifdef WIDE_CHAR_VERSION
  613. char str[c1];
  614. size_t strcnt;
  615. # else
  616. # define str (startp + 1)
  617. # endif
  618. const unsigned char *extra;
  619. int32_t idx;
  620. int32_t elem;
  621. int32_t second;
  622. int32_t hash;
  623. # ifdef WIDE_CHAR_VERSION
  624. /* We have to convert the name to a single-byte
  625. string. This is possible since the names
  626. consist of ASCII characters and the internal
  627. representation is UCS4. */
  628. for (strcnt = 0; strcnt < c1; ++strcnt)
  629. str[strcnt] = startp[1 + strcnt];
  630. # endif
  631. table_size =
  632. _NL_CURRENT_WORD (LC_COLLATE,
  633. _NL_COLLATE_SYMB_HASH_SIZEMB);
  634. symb_table = (const int32_t *)
  635. _NL_CURRENT (LC_COLLATE,
  636. _NL_COLLATE_SYMB_TABLEMB);
  637. extra = (const unsigned char *)
  638. _NL_CURRENT (LC_COLLATE,
  639. _NL_COLLATE_SYMB_EXTRAMB);
  640. /* Locate the character in the hashing
  641. table. */
  642. hash = elem_hash (str, c1);
  643. idx = 0;
  644. elem = hash % table_size;
  645. if (symb_table[2 * elem] != 0)
  646. {
  647. second = hash % (table_size - 2) + 1;
  648. do
  649. {
  650. /* First compare the hashing value. */
  651. if (symb_table[2 * elem] == hash
  652. && (c1
  653. == extra[symb_table[2 * elem + 1]])
  654. && memcmp (str,
  655. &extra[symb_table[2 * elem + 1]
  656. + 1], c1) == 0)
  657. {
  658. /* Yep, this is the entry. */
  659. idx = symb_table[2 * elem + 1];
  660. idx += 1 + extra[idx];
  661. break;
  662. }
  663. /* Next entry. */
  664. elem += second;
  665. }
  666. while (symb_table[2 * elem] != 0);
  667. }
  668. if (symb_table[2 * elem] != 0)
  669. {
  670. /* Compare the byte sequence but only if
  671. this is not part of a range. */
  672. # ifdef WIDE_CHAR_VERSION
  673. int32_t *wextra;
  674. idx += 1 + extra[idx];
  675. /* Adjust for the alignment. */
  676. idx = (idx + 3) & ~4;
  677. wextra = (int32_t *) &extra[idx + 4];
  678. # endif
  679. /* Get the collation sequence value. */
  680. is_seqval = true;
  681. # ifdef WIDE_CHAR_VERSION
  682. cend = wextra[1 + wextra[idx]];
  683. # else
  684. /* Adjust for the alignment. */
  685. idx += 1 + extra[idx];
  686. idx = (idx + 3) & ~4;
  687. cend = *((int32_t *) &extra[idx]);
  688. # endif
  689. }
  690. else if (symb_table[2 * elem] != 0 && c1 == 1)
  691. {
  692. cend = str[0];
  693. c = *p++;
  694. }
  695. else
  696. return FNM_NOMATCH;
  697. }
  698. # undef str
  699. }
  700. else
  701. {
  702. if (!(flags & FNM_NOESCAPE) && cend == L_('\\'))
  703. cend = *p++;
  704. if (cend == L_('\0'))
  705. return FNM_NOMATCH;
  706. cend = FOLD (cend);
  707. }
  708. /* XXX It is not entirely clear to me how to handle
  709. characters which are not mentioned in the
  710. collation specification. */
  711. if (
  712. # ifdef WIDE_CHAR_VERSION
  713. lcollseq == 0xffffffff ||
  714. # endif
  715. lcollseq <= fcollseq)
  716. {
  717. /* We have to look at the upper bound. */
  718. uint32_t hcollseq;
  719. if (is_seqval)
  720. hcollseq = cend;
  721. else
  722. {
  723. # ifdef WIDE_CHAR_VERSION
  724. hcollseq =
  725. __collseq_table_lookup (collseq, cend);
  726. if (hcollseq == ~((uint32_t) 0))
  727. {
  728. /* Hum, no information about the upper
  729. bound. The matching succeeds if the
  730. lower bound is matched exactly. */
  731. if (lcollseq != fcollseq)
  732. goto range_not_matched;
  733. goto matched;
  734. }
  735. # else
  736. hcollseq = collseq[cend];
  737. # endif
  738. }
  739. if (lcollseq <= hcollseq && fcollseq <= hcollseq)
  740. goto matched;
  741. }
  742. # ifdef WIDE_CHAR_VERSION
  743. range_not_matched:
  744. # endif
  745. #else
  746. /* We use a boring value comparison of the character
  747. values. This is better than comparing using
  748. 'strcoll' since the latter would have surprising
  749. and sometimes fatal consequences. */
  750. UCHAR cend = *p++;
  751. if (!(flags & FNM_NOESCAPE) && cend == L_('\\'))
  752. cend = *p++;
  753. if (cend == L_('\0'))
  754. return FNM_NOMATCH;
  755. /* It is a range. */
  756. if (cold <= fn && fn <= cend)
  757. goto matched;
  758. #endif
  759. c = *p++;
  760. }
  761. }
  762. if (c == L_(']'))
  763. break;
  764. }
  765. if (!not)
  766. return FNM_NOMATCH;
  767. break;
  768. matched:
  769. /* Skip the rest of the [...] that already matched. */
  770. do
  771. {
  772. ignore_next:
  773. c = *p++;
  774. if (c == L_('\0'))
  775. /* [... (unterminated) loses. */
  776. return FNM_NOMATCH;
  777. if (!(flags & FNM_NOESCAPE) && c == L_('\\'))
  778. {
  779. if (*p == L_('\0'))
  780. return FNM_NOMATCH;
  781. /* XXX 1003.2d11 is unclear if this is right. */
  782. ++p;
  783. }
  784. else if (c == L_('[') && *p == L_(':'))
  785. {
  786. int c1 = 0;
  787. const CHAR *startp = p;
  788. while (1)
  789. {
  790. c = *++p;
  791. if (++c1 == CHAR_CLASS_MAX_LENGTH)
  792. return FNM_NOMATCH;
  793. if (*p == L_(':') && p[1] == L_(']'))
  794. break;
  795. if (c < L_('a') || c >= L_('z'))
  796. {
  797. p = startp;
  798. goto ignore_next;
  799. }
  800. }
  801. p += 2;
  802. c = *p++;
  803. }
  804. else if (c == L_('[') && *p == L_('='))
  805. {
  806. c = *++p;
  807. if (c == L_('\0'))
  808. return FNM_NOMATCH;
  809. c = *++p;
  810. if (c != L_('=') || p[1] != L_(']'))
  811. return FNM_NOMATCH;
  812. p += 2;
  813. c = *p++;
  814. }
  815. else if (c == L_('[') && *p == L_('.'))
  816. {
  817. ++p;
  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 = true;
  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. bool no_leading_period, int flags)
  908. {
  909. const CHAR *startp;
  910. size_t level;
  911. struct patternlist
  912. {
  913. struct patternlist *next;
  914. CHAR str[1];
  915. } *list = NULL;
  916. struct patternlist **lastp = &list;
  917. size_t pattern_len = STRLEN (pattern);
  918. const CHAR *p;
  919. const CHAR *rs;
  920. enum { ALLOCA_LIMIT = 8000 };
  921. /* Parse the pattern. Store the individual parts in the list. */
  922. level = 0;
  923. for (startp = p = pattern + 1; ; ++p)
  924. if (*p == L_('\0'))
  925. /* This is an invalid pattern. */
  926. return -1;
  927. else if (*p == L_('['))
  928. {
  929. /* Handle brackets special. */
  930. if (posixly_correct == 0)
  931. posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
  932. /* Skip the not sign. We have to recognize it because of a possibly
  933. following ']'. */
  934. if (*++p == L_('!') || (posixly_correct < 0 && *p == L_('^')))
  935. ++p;
  936. /* A leading ']' is recognized as such. */
  937. if (*p == L_(']'))
  938. ++p;
  939. /* Skip over all characters of the list. */
  940. while (*p != L_(']'))
  941. if (*p++ == L_('\0'))
  942. /* This is no valid pattern. */
  943. return -1;
  944. }
  945. else if ((*p == L_('?') || *p == L_('*') || *p == L_('+') || *p == L_('@')
  946. || *p == L_('!')) && p[1] == L_('('))
  947. /* Remember the nesting level. */
  948. ++level;
  949. else if (*p == L_(')'))
  950. {
  951. if (level-- == 0)
  952. {
  953. /* This means we found the end of the pattern. */
  954. #define NEW_PATTERN \
  955. struct patternlist *newp; \
  956. size_t plen; \
  957. size_t plensize; \
  958. size_t newpsize; \
  959. \
  960. plen = (opt == L_('?') || opt == L_('@') \
  961. ? pattern_len \
  962. : p - startp + 1UL); \
  963. plensize = plen * sizeof (CHAR); \
  964. newpsize = offsetof (struct patternlist, str) + plensize; \
  965. if ((size_t) -1 / sizeof (CHAR) < plen \
  966. || newpsize < offsetof (struct patternlist, str) \
  967. || ALLOCA_LIMIT <= newpsize) \
  968. return -1; \
  969. newp = (struct patternlist *) alloca (newpsize); \
  970. *((CHAR *) MEMPCPY (newp->str, startp, p - startp)) = L_('\0'); \
  971. newp->next = NULL; \
  972. *lastp = newp; \
  973. lastp = &newp->next
  974. NEW_PATTERN;
  975. break;
  976. }
  977. }
  978. else if (*p == L_('|'))
  979. {
  980. if (level == 0)
  981. {
  982. NEW_PATTERN;
  983. startp = p + 1;
  984. }
  985. }
  986. assert (list != NULL);
  987. assert (p[-1] == L_(')'));
  988. #undef NEW_PATTERN
  989. switch (opt)
  990. {
  991. case L_('*'):
  992. if (FCT (p, string, string_end, no_leading_period, flags) == 0)
  993. return 0;
  994. /* FALLTHROUGH */
  995. case L_('+'):
  996. do
  997. {
  998. for (rs = string; rs <= string_end; ++rs)
  999. /* First match the prefix with the current pattern with the
  1000. current pattern. */
  1001. if (FCT (list->str, string, rs, no_leading_period,
  1002. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0
  1003. /* This was successful. Now match the rest with the rest
  1004. of the pattern. */
  1005. && (FCT (p, rs, string_end,
  1006. rs == string
  1007. ? no_leading_period
  1008. : rs[-1] == '/' && NO_LEADING_PERIOD (flags),
  1009. flags & FNM_FILE_NAME
  1010. ? flags : flags & ~FNM_PERIOD) == 0
  1011. /* This didn't work. Try the whole pattern. */
  1012. || (rs != string
  1013. && FCT (pattern - 1, rs, string_end,
  1014. rs == string
  1015. ? no_leading_period
  1016. : rs[-1] == '/' && NO_LEADING_PERIOD (flags),
  1017. flags & FNM_FILE_NAME
  1018. ? flags : flags & ~FNM_PERIOD) == 0)))
  1019. /* It worked. Signal success. */
  1020. return 0;
  1021. }
  1022. while ((list = list->next) != NULL);
  1023. /* None of the patterns lead to a match. */
  1024. return FNM_NOMATCH;
  1025. case L_('?'):
  1026. if (FCT (p, string, string_end, no_leading_period, flags) == 0)
  1027. return 0;
  1028. /* FALLTHROUGH */
  1029. case L_('@'):
  1030. do
  1031. /* I cannot believe it but 'strcat' is actually acceptable
  1032. here. Match the entire string with the prefix from the
  1033. pattern list and the rest of the pattern following the
  1034. pattern list. */
  1035. if (FCT (STRCAT (list->str, p), string, string_end,
  1036. no_leading_period,
  1037. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
  1038. /* It worked. Signal success. */
  1039. return 0;
  1040. while ((list = list->next) != NULL);
  1041. /* None of the patterns lead to a match. */
  1042. return FNM_NOMATCH;
  1043. case L_('!'):
  1044. for (rs = string; rs <= string_end; ++rs)
  1045. {
  1046. struct patternlist *runp;
  1047. for (runp = list; runp != NULL; runp = runp->next)
  1048. if (FCT (runp->str, string, rs, no_leading_period,
  1049. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
  1050. break;
  1051. /* If none of the patterns matched see whether the rest does. */
  1052. if (runp == NULL
  1053. && (FCT (p, rs, string_end,
  1054. rs == string
  1055. ? no_leading_period
  1056. : rs[-1] == '/' && NO_LEADING_PERIOD (flags),
  1057. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD)
  1058. == 0))
  1059. /* This is successful. */
  1060. return 0;
  1061. }
  1062. /* None of the patterns together with the rest of the pattern
  1063. lead to a match. */
  1064. return FNM_NOMATCH;
  1065. default:
  1066. assert (! "Invalid extended matching operator");
  1067. break;
  1068. }
  1069. return -1;
  1070. }
  1071. #undef FOLD
  1072. #undef CHAR
  1073. #undef UCHAR
  1074. #undef INT
  1075. #undef FCT
  1076. #undef EXT
  1077. #undef END
  1078. #undef MEMPCPY
  1079. #undef MEMCHR
  1080. #undef STRLEN
  1081. #undef STRCAT
  1082. #undef L_
  1083. #undef BTOWC