fnmatch_loop.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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. ++p;
  819. while (1)
  820. {
  821. c = *++p;
  822. if (c == '\0')
  823. return FNM_NOMATCH;
  824. if (*p == L('.') && p[1] == L(']'))
  825. break;
  826. }
  827. p += 2;
  828. c = *p++;
  829. }
  830. }
  831. while (c != L(']'));
  832. if (not)
  833. return FNM_NOMATCH;
  834. }
  835. break;
  836. case L('+'):
  837. case L('@'):
  838. case L('!'):
  839. if (__builtin_expect (flags & FNM_EXTMATCH, 0) && *p == '(')
  840. {
  841. int res;
  842. res = EXT (c, p, n, string_end, no_leading_period, flags);
  843. if (res != -1)
  844. return res;
  845. }
  846. goto normal_match;
  847. case L('/'):
  848. if (NO_LEADING_PERIOD (flags))
  849. {
  850. if (n == string_end || c != (UCHAR) *n)
  851. return FNM_NOMATCH;
  852. new_no_leading_period = 1;
  853. break;
  854. }
  855. /* FALLTHROUGH */
  856. default:
  857. normal_match:
  858. if (n == string_end || c != FOLD ((UCHAR) *n))
  859. return FNM_NOMATCH;
  860. }
  861. no_leading_period = new_no_leading_period;
  862. ++n;
  863. }
  864. if (n == string_end)
  865. return 0;
  866. if ((flags & FNM_LEADING_DIR) && n != string_end && *n == L('/'))
  867. /* The FNM_LEADING_DIR flag says that "foo*" matches "foobar/frobozz". */
  868. return 0;
  869. return FNM_NOMATCH;
  870. }
  871. static const CHAR *
  872. internal_function
  873. END (const CHAR *pattern)
  874. {
  875. const CHAR *p = pattern;
  876. while (1)
  877. if (*++p == L('\0'))
  878. /* This is an invalid pattern. */
  879. return pattern;
  880. else if (*p == L('['))
  881. {
  882. /* Handle brackets special. */
  883. if (posixly_correct == 0)
  884. posixly_correct = getenv ("POSIXLY_CORRECT") != NULL ? 1 : -1;
  885. /* Skip the not sign. We have to recognize it because of a possibly
  886. following ']'. */
  887. if (*++p == L('!') || (posixly_correct < 0 && *p == L('^')))
  888. ++p;
  889. /* A leading ']' is recognized as such. */
  890. if (*p == L(']'))
  891. ++p;
  892. /* Skip over all characters of the list. */
  893. while (*p != L(']'))
  894. if (*p++ == L('\0'))
  895. /* This is no valid pattern. */
  896. return pattern;
  897. }
  898. else if ((*p == L('?') || *p == L('*') || *p == L('+') || *p == L('@')
  899. || *p == L('!')) && p[1] == L('('))
  900. p = END (p + 1);
  901. else if (*p == L(')'))
  902. break;
  903. return p + 1;
  904. }
  905. static int
  906. internal_function
  907. EXT (INT opt, const CHAR *pattern, const CHAR *string, const CHAR *string_end,
  908. int no_leading_period, int flags)
  909. {
  910. const CHAR *startp;
  911. int level;
  912. struct patternlist
  913. {
  914. struct patternlist *next;
  915. CHAR str[0];
  916. } *list = NULL;
  917. struct patternlist **lastp = &list;
  918. size_t pattern_len = STRLEN (pattern);
  919. const CHAR *p;
  920. const CHAR *rs;
  921. /* Parse the pattern. Store the individual parts in the list. */
  922. level = 0;
  923. for (startp = p = pattern + 1; level >= 0; ++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. \
  957. if (opt == L('?') || opt == L('@')) \
  958. newp = alloca (sizeof (struct patternlist) \
  959. + (pattern_len * sizeof (CHAR))); \
  960. else \
  961. newp = alloca (sizeof (struct patternlist) \
  962. + ((p - startp + 1) * sizeof (CHAR))); \
  963. *((CHAR *) MEMPCPY (newp->str, startp, p - startp)) = L('\0'); \
  964. newp->next = NULL; \
  965. *lastp = newp; \
  966. lastp = &newp->next
  967. NEW_PATTERN;
  968. }
  969. }
  970. else if (*p == L('|'))
  971. {
  972. if (level == 0)
  973. {
  974. NEW_PATTERN;
  975. startp = p + 1;
  976. }
  977. }
  978. assert (list != NULL);
  979. assert (p[-1] == L(')'));
  980. #undef NEW_PATTERN
  981. switch (opt)
  982. {
  983. case L('*'):
  984. if (FCT (p, string, string_end, no_leading_period, flags) == 0)
  985. return 0;
  986. /* FALLTHROUGH */
  987. case L('+'):
  988. do
  989. {
  990. for (rs = string; rs <= string_end; ++rs)
  991. /* First match the prefix with the current pattern with the
  992. current pattern. */
  993. if (FCT (list->str, string, rs, no_leading_period,
  994. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0
  995. /* This was successful. Now match the rest with the rest
  996. of the pattern. */
  997. && (FCT (p, rs, string_end,
  998. rs == string
  999. ? no_leading_period
  1000. : rs[-1] == '/' && NO_LEADING_PERIOD (flags) ? 1 : 0,
  1001. flags & FNM_FILE_NAME
  1002. ? flags : flags & ~FNM_PERIOD) == 0
  1003. /* This didn't work. Try the whole pattern. */
  1004. || (rs != string
  1005. && FCT (pattern - 1, rs, string_end,
  1006. rs == string
  1007. ? no_leading_period
  1008. : (rs[-1] == '/' && NO_LEADING_PERIOD (flags)
  1009. ? 1 : 0),
  1010. flags & FNM_FILE_NAME
  1011. ? flags : flags & ~FNM_PERIOD) == 0)))
  1012. /* It worked. Signal success. */
  1013. return 0;
  1014. }
  1015. while ((list = list->next) != NULL);
  1016. /* None of the patterns lead to a match. */
  1017. return FNM_NOMATCH;
  1018. case L('?'):
  1019. if (FCT (p, string, string_end, no_leading_period, flags) == 0)
  1020. return 0;
  1021. /* FALLTHROUGH */
  1022. case L('@'):
  1023. do
  1024. /* I cannot believe it but `strcat' is actually acceptable
  1025. here. Match the entire string with the prefix from the
  1026. pattern list and the rest of the pattern following the
  1027. pattern list. */
  1028. if (FCT (STRCAT (list->str, p), string, string_end,
  1029. no_leading_period,
  1030. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
  1031. /* It worked. Signal success. */
  1032. return 0;
  1033. while ((list = list->next) != NULL);
  1034. /* None of the patterns lead to a match. */
  1035. return FNM_NOMATCH;
  1036. case L('!'):
  1037. for (rs = string; rs <= string_end; ++rs)
  1038. {
  1039. struct patternlist *runp;
  1040. for (runp = list; runp != NULL; runp = runp->next)
  1041. if (FCT (runp->str, string, rs, no_leading_period,
  1042. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD) == 0)
  1043. break;
  1044. /* If none of the patterns matched see whether the rest does. */
  1045. if (runp == NULL
  1046. && (FCT (p, rs, string_end,
  1047. rs == string
  1048. ? no_leading_period
  1049. : rs[-1] == '/' && NO_LEADING_PERIOD (flags) ? 1 : 0,
  1050. flags & FNM_FILE_NAME ? flags : flags & ~FNM_PERIOD)
  1051. == 0))
  1052. /* This is successful. */
  1053. return 0;
  1054. }
  1055. /* None of the patterns together with the rest of the pattern
  1056. lead to a match. */
  1057. return FNM_NOMATCH;
  1058. default:
  1059. assert (! "Invalid extended matching operator");
  1060. break;
  1061. }
  1062. return -1;
  1063. }
  1064. #undef FOLD
  1065. #undef CHAR
  1066. #undef UCHAR
  1067. #undef INT
  1068. #undef FCT
  1069. #undef EXT
  1070. #undef END
  1071. #undef MEMPCPY
  1072. #undef MEMCHR
  1073. #undef STRCOLL
  1074. #undef STRLEN
  1075. #undef STRCAT
  1076. #undef L
  1077. #undef BTOWC