fnmatch_loop.c 29 KB

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