tst-fnmatch.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. 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. else if (strncasecmp (str, "CASEFOLD", 8) == 0
  256. && (str[8] == '|' || str[8] == '\0'))
  257. {
  258. result |= FNM_CASEFOLD;
  259. len = 8;
  260. }
  261. else if (strncasecmp (str, "EXTMATCH", 8) == 0
  262. && (str[8] == '|' || str[8] == '\0'))
  263. {
  264. result |= FNM_EXTMATCH;
  265. len = 8;
  266. }
  267. else
  268. return -1;
  269. str += len;
  270. if (*str != '\0')
  271. ++str;
  272. }
  273. return result;
  274. }
  275. static char *
  276. flag_output (int flags)
  277. {
  278. static char buf[100];
  279. int first = 1;
  280. char *cp = buf;
  281. if (flags & FNM_PATHNAME)
  282. {
  283. cp = stpcpy (cp, "FNM_PATHNAME");
  284. first = 0;
  285. }
  286. if (flags & FNM_NOESCAPE)
  287. {
  288. if (! first)
  289. *cp++ = '|';
  290. cp = stpcpy (cp, "FNM_NOESCAPE");
  291. first = 0;
  292. }
  293. if (flags & FNM_PERIOD)
  294. {
  295. if (! first)
  296. *cp++ = '|';
  297. cp = stpcpy (cp, "FNM_PERIOD");
  298. first = 0;
  299. }
  300. if (flags & FNM_LEADING_DIR)
  301. {
  302. if (! first)
  303. *cp++ = '|';
  304. cp = stpcpy (cp, "FNM_LEADING_DIR");
  305. first = 0;
  306. }
  307. if (flags & FNM_CASEFOLD)
  308. {
  309. if (! first)
  310. *cp++ = '|';
  311. cp = stpcpy (cp, "FNM_CASEFOLD");
  312. first = 0;
  313. }
  314. if (flags & FNM_EXTMATCH)
  315. {
  316. if (! first)
  317. *cp++ = '|';
  318. cp = stpcpy (cp, "FNM_EXTMATCH");
  319. first = 0;
  320. }
  321. if (cp == buf)
  322. *cp++ = '0';
  323. *cp = '\0';
  324. return buf;
  325. }
  326. static char *
  327. escape (const char *str, size_t *reslenp, char **resbufp)
  328. {
  329. size_t reslen = *reslenp;
  330. char *resbuf = *resbufp;
  331. size_t len = strlen (str);
  332. char *wp;
  333. if (2 * len + 1 > reslen)
  334. {
  335. resbuf = (char *) realloc (resbuf, 2 * len + 1);
  336. if (resbuf == NULL)
  337. error (EXIT_FAILURE, errno, "while allocating buffer for printing");
  338. *reslenp = 2 * len + 1;
  339. *resbufp = resbuf;
  340. }
  341. wp = resbuf;
  342. while (*str != '\0')
  343. if (*str == '\t')
  344. {
  345. *wp++ = '\\';
  346. *wp++ = 't';
  347. ++str;
  348. }
  349. else if (*str == '\n')
  350. {
  351. *wp++ = '\\';
  352. *wp++ = 'n';
  353. ++str;
  354. }
  355. else if (*str == '"')
  356. {
  357. *wp++ = '\\';
  358. *wp++ = '"';
  359. ++str;
  360. }
  361. else if (*str == '\\')
  362. {
  363. *wp++ = '\\';
  364. *wp++ = '\\';
  365. ++str;
  366. }
  367. else
  368. *wp++ = *str++;
  369. *wp = '\0';
  370. return resbuf;
  371. }