tst-fnmatch.c 7.8 KB

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