1 /* Tests for fnmatch function.
2 Copyright (C) 2000, 2001, 2010 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <sys/types.h>
31 static char *next_input (char **line, int first, int last);
32 static int convert_flags (const char *str);
33 static char *flag_output (int flags);
34 static char *escape (const char *str, size_t *reslenp, char **resbuf);
41 size_t linebuflen = 0;
44 char *escinput = NULL;
45 size_t escinputlen = 0;
46 char *escpattern = NULL;
47 size_t escpatternlen = 0;
52 /* Read lines from stdin with the following format:
54 locale input-string match-string flags result
56 where `result' is either 0 or 1. If the first character of a
57 string is '"' we read until the next '"' and handled escaped '"'. */
58 while (! feof (stdin))
60 ssize_t n = getline (&linebuf, &linebuflen, stdin);
65 const char *result_str;
76 /* Maybe an empty line. */
79 /* Skip over all leading white spaces. */
82 locale = next_input (&cp, 1, 0);
86 input = next_input (&cp, 0, 0);
90 pattern = next_input (&cp, 0, 0);
94 result_str = next_input (&cp, 0, 0);
95 if (result_str == NULL)
98 if (strcmp (result_str, "0") == 0)
100 else if (strcasecmp (result_str, "NOMATCH") == 0)
101 result = FNM_NOMATCH;
105 result = strtol (result_str, &endp, 0);
110 flags = next_input (&cp, 0, 1);
112 /* We allow the flags missing. */
115 /* Convert the text describing the flags in a numeric value. */
116 flags_val = convert_flags (flags);
118 /* Something went wrong. */
121 /* Now run the actual test. */
124 if (setlocale (LC_COLLATE, locale) == NULL
125 || setlocale (LC_CTYPE, locale) == NULL)
127 puts ("*** Cannot set locale");
132 fnmres = fnmatch (pattern, input, flags_val);
134 printf ("%3d: fnmatch (\"%s\", \"%s\", %s) = %s%c",
136 escape (pattern, &escpatternlen, &escpattern),
137 escape (input, &escinputlen, &escinput),
138 flag_output (flags_val),
140 ? "0" : (fnmres == FNM_NOMATCH
142 : (sprintf (numbuf, "%d", fnmres), numbuf))),
143 (fnmres != 0) != (result != 0) ? ' ' : '\n');
145 if ((fnmres != 0) != (result != 0))
147 printf ("(FAIL, expected %s) ***\n",
149 ? "0" : (result == FNM_NOMATCH
151 : (sprintf (numbuf, "%d", result), numbuf)));
156 printf ("=====================\n%3d tests, %3d failed\n", ntests, nfailed);
167 next_input (char **line, int first, int last)
172 while (*cp == ' ' || *cp == '\t')
175 /* We allow comment lines starting with '#'. */
176 if (first && *cp == '#')
186 while (*cp != '"' && *cp != '\0' && *cp != '\n')
189 if (cp[1] == '\n' || cp[1] == '\0')
214 while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t')
217 if (cp == result && ! last)
218 /* Premature end of line. */
222 /* Terminate and skip over the next white spaces. */
231 convert_flags (const char *str)
239 if (strncasecmp (str, "PATHNAME", 8) == 0
240 && (str[8] == '|' || str[8] == '\0'))
242 result |= FNM_PATHNAME;
245 else if (strncasecmp (str, "NOESCAPE", 8) == 0
246 && (str[8] == '|' || str[8] == '\0'))
248 result |= FNM_NOESCAPE;
251 else if (strncasecmp (str, "PERIOD", 6) == 0
252 && (str[6] == '|' || str[6] == '\0'))
254 result |= FNM_PERIOD;
257 else if (strncasecmp (str, "LEADING_DIR", 11) == 0
258 && (str[11] == '|' || str[11] == '\0'))
260 result |= FNM_LEADING_DIR;
263 else if (strncasecmp (str, "CASEFOLD", 8) == 0
264 && (str[8] == '|' || str[8] == '\0'))
266 result |= FNM_CASEFOLD;
269 else if (strncasecmp (str, "EXTMATCH", 8) == 0
270 && (str[8] == '|' || str[8] == '\0'))
272 result |= FNM_EXTMATCH;
288 flag_output (int flags)
290 static char buf[100];
294 if (flags & FNM_PATHNAME)
296 cp = stpcpy (cp, "FNM_PATHNAME");
299 if (flags & FNM_NOESCAPE)
303 cp = stpcpy (cp, "FNM_NOESCAPE");
306 if (flags & FNM_PERIOD)
310 cp = stpcpy (cp, "FNM_PERIOD");
313 if (flags & FNM_LEADING_DIR)
317 cp = stpcpy (cp, "FNM_LEADING_DIR");
320 if (flags & FNM_CASEFOLD)
324 cp = stpcpy (cp, "FNM_CASEFOLD");
327 if (flags & FNM_EXTMATCH)
331 cp = stpcpy (cp, "FNM_EXTMATCH");
343 escape (const char *str, size_t *reslenp, char **resbufp)
345 size_t reslen = *reslenp;
346 char *resbuf = *resbufp;
347 size_t len = strlen (str);
350 if (2 * len + 1 > reslen)
352 resbuf = (char *) realloc (resbuf, 2 * len + 1);
354 error (EXIT_FAILURE, errno, "while allocating buffer for printing");
355 *reslenp = 2 * len + 1;
367 else if (*str == '\n')
373 else if (*str == '"')
379 else if (*str == '\\')