tst-fnmatch.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /* Tests for fnmatch function.
  2. Copyright (C) 2000, 2001 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. #include <errno.h>
  16. #include <error.h>
  17. #include <fnmatch.h>
  18. #include <locale.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <sys/types.h>
  24. static char *next_input (char **line, int first, int last);
  25. static int convert_flags (const char *str);
  26. static char *flag_output (int flags);
  27. static char *escape (const char *str, size_t *reslenp, char **resbuf);
  28. int str_isalpha(const char *str)
  29. {
  30. size_t i = strlen(str);
  31. while (i--)
  32. if (isascii(str[i]) == 0)
  33. return 0;
  34. return 1;
  35. }
  36. int str_has_funk(const char *str, const char x)
  37. {
  38. size_t i, max = strlen(str);
  39. for (i=0; i+1<max; ++i)
  40. if (str[i] == '[' && str[i+1] == x)
  41. return 1;
  42. return 0;
  43. }
  44. int
  45. main (void)
  46. {
  47. char *linebuf = NULL;
  48. size_t linebuflen = 0;
  49. int ntests = 0;
  50. int nfailed = 0;
  51. int nskipped = 0;
  52. char *escinput = NULL;
  53. size_t escinputlen = 0;
  54. char *escpattern = NULL;
  55. size_t escpatternlen = 0;
  56. int nr = 0;
  57. /* Read lines from stdin with the following format:
  58. locale input-string match-string flags result
  59. where `result' is either 0 or 1. If the first character of a
  60. string is '"' we read until the next '"' and handled escaped '"'. */
  61. while (! feof (stdin))
  62. {
  63. ssize_t n = getline (&linebuf, &linebuflen, stdin);
  64. char *cp;
  65. const char *locale;
  66. const char *input;
  67. const char *pattern;
  68. const char *result_str;
  69. int result;
  70. const char *flags;
  71. int flags_val;
  72. int fnmres;
  73. char numbuf[24];
  74. if (n == -1)
  75. break;
  76. if (n == 0)
  77. /* Maybe an empty line. */
  78. continue;
  79. /* Skip over all leading white spaces. */
  80. cp = linebuf;
  81. locale = next_input (&cp, 1, 0);
  82. if (locale == NULL)
  83. continue;
  84. input = next_input (&cp, 0, 0);
  85. if (input == NULL)
  86. continue;
  87. pattern = next_input (&cp, 0, 0);
  88. if (pattern == NULL)
  89. continue;
  90. result_str = next_input (&cp, 0, 0);
  91. if (result_str == NULL)
  92. continue;
  93. if (strcmp (result_str, "0") == 0)
  94. result = 0;
  95. else if (strcasecmp (result_str, "NOMATCH") == 0)
  96. result = FNM_NOMATCH;
  97. else
  98. {
  99. char *endp;
  100. result = strtol (result_str, &endp, 0);
  101. if (*endp != '\0')
  102. continue;
  103. }
  104. flags = next_input (&cp, 0, 1);
  105. if (flags == NULL)
  106. /* We allow the flags missing. */
  107. flags = "";
  108. /* Convert the text describing the flags in a numeric value. */
  109. flags_val = convert_flags (flags);
  110. if (flags_val == -1)
  111. /* Something went wrong. */
  112. continue;
  113. /* Now run the actual test. */
  114. ++ntests;
  115. #ifdef __UCLIBC_HAS_XLOCALE__
  116. if (setlocale (LC_COLLATE, locale) == NULL
  117. || setlocale (LC_CTYPE, locale) == NULL)
  118. {
  119. puts ("*** Cannot set locale");
  120. ++nfailed;
  121. continue;
  122. }
  123. #else
  124. /* skip non-ascii strings */
  125. if (!str_isalpha(pattern) || !str_isalpha(input))
  126. {
  127. ++nskipped;
  128. printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP multibyte test (requires locale support)\n", ++nr, pattern, input);
  129. continue;
  130. }
  131. /* skip collating symbols */
  132. if (str_has_funk(pattern, '.') || str_has_funk(input, '.'))
  133. {
  134. ++nskipped;
  135. printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP collating symbol test (requires locale support)\n", ++nr, pattern, input);
  136. continue;
  137. }
  138. /* skip equivalence class expressions */
  139. if (str_has_funk(pattern, '=') || str_has_funk(input, '='))
  140. {
  141. ++nskipped;
  142. printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP equivalence class test (requires locale support)\n", ++nr, pattern, input);
  143. continue;
  144. }
  145. #endif
  146. fnmres = fnmatch (pattern, input, flags_val);
  147. printf ("%3d: fnmatch (\"%s\", \"%s\", %s) = %s%c",
  148. ++nr,
  149. escape (pattern, &escpatternlen, &escpattern),
  150. escape (input, &escinputlen, &escinput),
  151. flag_output (flags_val),
  152. (fnmres == 0
  153. ? "0" : (fnmres == FNM_NOMATCH
  154. ? "FNM_NOMATCH"
  155. : (sprintf (numbuf, "%d", fnmres), numbuf))),
  156. (fnmres != 0) != (result != 0) ? ' ' : '\n');
  157. if ((fnmres != 0) != (result != 0))
  158. {
  159. printf ("(FAIL, expected %s) ***\n",
  160. result == 0
  161. ? "0" : (result == FNM_NOMATCH
  162. ? "FNM_NOMATCH"
  163. : (sprintf (numbuf, "%d", result), numbuf)));
  164. ++nfailed;
  165. }
  166. }
  167. printf ("=====================\n%3d tests, %3d failed, %3d skipped\n", ntests, nfailed, nskipped);
  168. free (escpattern);
  169. free (escinput);
  170. free (linebuf);
  171. return nfailed != 0;
  172. }
  173. static char *
  174. next_input (char **line, int first, int last)
  175. {
  176. char *cp = *line;
  177. char *result;
  178. while (*cp == ' ' || *cp == '\t')
  179. ++cp;
  180. /* We allow comment lines starting with '#'. */
  181. if (first && *cp == '#')
  182. return NULL;
  183. if (*cp == '"')
  184. {
  185. char *wp;
  186. result = ++cp;
  187. wp = cp;
  188. while (*cp != '"' && *cp != '\0' && *cp != '\n')
  189. if (*cp == '\\')
  190. {
  191. if (cp[1] == '\n' || cp[1] == '\0')
  192. return NULL;
  193. ++cp;
  194. if (*cp == 't')
  195. *wp++ = '\t';
  196. else if (*cp == 'n')
  197. *wp++ = '\n';
  198. else
  199. *wp++ = *cp;
  200. ++cp;
  201. }
  202. else
  203. *wp++ = *cp++;
  204. if (*cp != '"')
  205. return NULL;
  206. if (wp != cp)
  207. *wp = '\0';
  208. }
  209. else
  210. {
  211. result = cp;
  212. while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t')
  213. ++cp;
  214. if (cp == result && ! last)
  215. /* Premature end of line. */
  216. return NULL;
  217. }
  218. /* Terminate and skip over the next white spaces. */
  219. *cp++ = '\0';
  220. *line = cp;
  221. return result;
  222. }
  223. static int
  224. convert_flags (const char *str)
  225. {
  226. int result = 0;
  227. while (*str != '\0')
  228. {
  229. int len;
  230. if (strncasecmp (str, "PATHNAME", 8) == 0
  231. && (str[8] == '|' || str[8] == '\0'))
  232. {
  233. result |= FNM_PATHNAME;
  234. len = 8;
  235. }
  236. else if (strncasecmp (str, "NOESCAPE", 8) == 0
  237. && (str[8] == '|' || str[8] == '\0'))
  238. {
  239. result |= FNM_NOESCAPE;
  240. len = 8;
  241. }
  242. else if (strncasecmp (str, "PERIOD", 6) == 0
  243. && (str[6] == '|' || str[6] == '\0'))
  244. {
  245. result |= FNM_PERIOD;
  246. len = 6;
  247. }
  248. #ifdef FNM_LEADING_DIR
  249. else if (strncasecmp (str, "LEADING_DIR", 11) == 0
  250. && (str[11] == '|' || str[11] == '\0'))
  251. {
  252. result |= FNM_LEADING_DIR;
  253. len = 11;
  254. }
  255. #endif
  256. #ifdef FNM_CASEFOLD
  257. else if (strncasecmp (str, "CASEFOLD", 8) == 0
  258. && (str[8] == '|' || str[8] == '\0'))
  259. {
  260. result |= FNM_CASEFOLD;
  261. len = 8;
  262. }
  263. #endif
  264. #ifdef FNM_EXTMATCH
  265. else if (strncasecmp (str, "EXTMATCH", 8) == 0
  266. && (str[8] == '|' || str[8] == '\0'))
  267. {
  268. result |= FNM_EXTMATCH;
  269. len = 8;
  270. }
  271. #endif
  272. else
  273. return -1;
  274. str += len;
  275. if (*str != '\0')
  276. ++str;
  277. }
  278. return result;
  279. }
  280. static char *
  281. flag_output (int flags)
  282. {
  283. static char buf[100];
  284. int first = 1;
  285. char *cp = buf;
  286. if (flags & FNM_PATHNAME)
  287. {
  288. cp = stpcpy (cp, "FNM_PATHNAME");
  289. first = 0;
  290. }
  291. if (flags & FNM_NOESCAPE)
  292. {
  293. if (! first)
  294. *cp++ = '|';
  295. cp = stpcpy (cp, "FNM_NOESCAPE");
  296. first = 0;
  297. }
  298. if (flags & FNM_PERIOD)
  299. {
  300. if (! first)
  301. *cp++ = '|';
  302. cp = stpcpy (cp, "FNM_PERIOD");
  303. first = 0;
  304. }
  305. #ifdef FNM_LEADING_DIR
  306. if (flags & FNM_LEADING_DIR)
  307. {
  308. if (! first)
  309. *cp++ = '|';
  310. cp = stpcpy (cp, "FNM_LEADING_DIR");
  311. first = 0;
  312. }
  313. #endif
  314. #ifdef FNM_CASEFOLD
  315. if (flags & FNM_CASEFOLD)
  316. {
  317. if (! first)
  318. *cp++ = '|';
  319. cp = stpcpy (cp, "FNM_CASEFOLD");
  320. first = 0;
  321. }
  322. #endif
  323. #ifdef FNM_EXTMATCH
  324. if (flags & FNM_EXTMATCH)
  325. {
  326. if (! first)
  327. *cp++ = '|';
  328. cp = stpcpy (cp, "FNM_EXTMATCH");
  329. first = 0;
  330. }
  331. #endif
  332. if (cp == buf)
  333. *cp++ = '0';
  334. *cp = '\0';
  335. return buf;
  336. }
  337. static char *
  338. escape (const char *str, size_t *reslenp, char **resbufp)
  339. {
  340. size_t reslen = *reslenp;
  341. char *resbuf = *resbufp;
  342. size_t len = strlen (str);
  343. char *wp;
  344. if (2 * len + 1 > reslen)
  345. {
  346. resbuf = (char *) realloc (resbuf, 2 * len + 1);
  347. if (resbuf == NULL)
  348. error (EXIT_FAILURE, errno, "while allocating buffer for printing");
  349. *reslenp = 2 * len + 1;
  350. *resbufp = resbuf;
  351. }
  352. wp = resbuf;
  353. while (*str != '\0')
  354. if (*str == '\t')
  355. {
  356. *wp++ = '\\';
  357. *wp++ = 't';
  358. ++str;
  359. }
  360. else if (*str == '\n')
  361. {
  362. *wp++ = '\\';
  363. *wp++ = 'n';
  364. ++str;
  365. }
  366. else if (*str == '"')
  367. {
  368. *wp++ = '\\';
  369. *wp++ = '"';
  370. ++str;
  371. }
  372. else if (*str == '\\')
  373. {
  374. *wp++ = '\\';
  375. *wp++ = '\\';
  376. ++str;
  377. }
  378. else
  379. *wp++ = *str++;
  380. *wp = '\0';
  381. return resbuf;
  382. }