tst-fnmatch.c 9.1 KB

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