1 /* Tests for fnmatch function.
2 Copyright (C) 2000, 2001 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>
30 static char *next_input (char **line, int first, int last);
31 static int convert_flags (const char *str);
32 static char *flag_output (int flags);
33 static char *escape (const char *str, size_t *reslenp, char **resbuf);
40 size_t linebuflen = 0;
43 char *escinput = NULL;
44 size_t escinputlen = 0;
45 char *escpattern = NULL;
46 size_t escpatternlen = 0;
49 /* Read lines from stdin with the following format:
51 locale input-string match-string flags result
53 where `result' is either 0 or 1. If the first character of a
54 string is '"' we read until the next '"' and handled escaped '"'. */
55 while (! feof (stdin))
57 ssize_t n = getline (&linebuf, &linebuflen, stdin);
62 const char *result_str;
73 /* Maybe an empty line. */
76 /* Skip over all leading white spaces. */
79 locale = next_input (&cp, 1, 0);
83 input = next_input (&cp, 0, 0);
87 pattern = next_input (&cp, 0, 0);
91 result_str = next_input (&cp, 0, 0);
92 if (result_str == NULL)
95 if (strcmp (result_str, "0") == 0)
97 else if (strcasecmp (result_str, "NOMATCH") == 0)
102 result = strtol (result_str, &endp, 0);
107 flags = next_input (&cp, 0, 1);
109 /* We allow the flags missing. */
112 /* Convert the text describing the flags in a numeric value. */
113 flags_val = convert_flags (flags);
115 /* Something went wrong. */
118 /* Now run the actual test. */
121 if (setlocale (LC_COLLATE, locale) == NULL
122 || setlocale (LC_CTYPE, locale) == NULL)
124 puts ("*** Cannot set locale");
129 fnmres = fnmatch (pattern, input, flags_val);
131 printf ("%3d: fnmatch (\"%s\", \"%s\", %s) = %s%c",
133 escape (pattern, &escpatternlen, &escpattern),
134 escape (input, &escinputlen, &escinput),
135 flag_output (flags_val),
137 ? "0" : (fnmres == FNM_NOMATCH
139 : (sprintf (numbuf, "%d", fnmres), numbuf))),
140 (fnmres != 0) != (result != 0) ? ' ' : '\n');
142 if ((fnmres != 0) != (result != 0))
144 printf ("(FAIL, expected %s) ***\n",
146 ? "0" : (result == FNM_NOMATCH
148 : (sprintf (numbuf, "%d", result), numbuf)));
153 printf ("=====================\n%3d tests, %3d failed\n", ntests, nfailed);
164 next_input (char **line, int first, int last)
169 while (*cp == ' ' || *cp == '\t')
172 /* We allow comment lines starting with '#'. */
173 if (first && *cp == '#')
183 while (*cp != '"' && *cp != '\0' && *cp != '\n')
186 if (cp[1] == '\n' || cp[1] == '\0')
211 while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t')
214 if (cp == result && ! last)
215 /* Premature end of line. */
219 /* Terminate and skip over the next white spaces. */
228 convert_flags (const char *str)
236 if (strncasecmp (str, "PATHNAME", 8) == 0
237 && (str[8] == '|' || str[8] == '\0'))
239 result |= FNM_PATHNAME;
242 else if (strncasecmp (str, "NOESCAPE", 8) == 0
243 && (str[8] == '|' || str[8] == '\0'))
245 result |= FNM_NOESCAPE;
248 else if (strncasecmp (str, "PERIOD", 6) == 0
249 && (str[6] == '|' || str[6] == '\0'))
251 result |= FNM_PERIOD;
254 else if (strncasecmp (str, "LEADING_DIR", 11) == 0
255 && (str[11] == '|' || str[11] == '\0'))
257 result |= FNM_LEADING_DIR;
260 else if (strncasecmp (str, "CASEFOLD", 8) == 0
261 && (str[8] == '|' || str[8] == '\0'))
263 result |= FNM_CASEFOLD;
266 else if (strncasecmp (str, "EXTMATCH", 8) == 0
267 && (str[8] == '|' || str[8] == '\0'))
269 result |= FNM_EXTMATCH;
285 flag_output (int flags)
287 static char buf[100];
291 if (flags & FNM_PATHNAME)
293 cp = stpcpy (cp, "FNM_PATHNAME");
296 if (flags & FNM_NOESCAPE)
300 cp = stpcpy (cp, "FNM_NOESCAPE");
303 if (flags & FNM_PERIOD)
307 cp = stpcpy (cp, "FNM_PERIOD");
310 if (flags & FNM_LEADING_DIR)
314 cp = stpcpy (cp, "FNM_LEADING_DIR");
317 if (flags & FNM_CASEFOLD)
321 cp = stpcpy (cp, "FNM_CASEFOLD");
324 if (flags & FNM_EXTMATCH)
328 cp = stpcpy (cp, "FNM_EXTMATCH");
340 escape (const char *str, size_t *reslenp, char **resbufp)
342 size_t reslen = *reslenp;
343 char *resbuf = *resbufp;
344 size_t len = strlen (str);
347 if (2 * len + 1 > reslen)
349 resbuf = (char *) realloc (resbuf, 2 * len + 1);
351 error (EXIT_FAILURE, errno, "while allocating buffer for printing");
352 *reslenp = 2 * len + 1;
364 else if (*str == '\n')
370 else if (*str == '"')
376 else if (*str == '\\')