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, see
17 <http://www.gnu.org/licenses/>. */
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);
36 int str_isalpha(const char *str)
38 size_t i = strlen(str);
40 if (isascii(str[i]) == 0)
44 int str_has_funk(const char *str, const char x)
46 size_t i, max = strlen(str);
47 for (i=0; i+1<max; ++i)
48 if (str[i] == '[' && str[i+1] == x)
58 size_t linebuflen = 0;
62 char *escinput = NULL;
63 size_t escinputlen = 0;
64 char *escpattern = NULL;
65 size_t escpatternlen = 0;
68 /* Read lines from stdin with the following format:
70 locale input-string match-string flags result
72 where `result' is either 0 or 1. If the first character of a
73 string is '"' we read until the next '"' and handled escaped '"'. */
74 while (! feof (stdin))
76 ssize_t n = getline (&linebuf, &linebuflen, stdin);
81 const char *result_str;
92 /* Maybe an empty line. */
95 /* Skip over all leading white spaces. */
98 locale = next_input (&cp, 1, 0);
102 input = next_input (&cp, 0, 0);
106 pattern = next_input (&cp, 0, 0);
110 result_str = next_input (&cp, 0, 0);
111 if (result_str == NULL)
114 if (strcmp (result_str, "0") == 0)
116 else if (strcasecmp (result_str, "NOMATCH") == 0)
117 result = FNM_NOMATCH;
121 result = strtol (result_str, &endp, 0);
126 flags = next_input (&cp, 0, 1);
128 /* We allow the flags missing. */
131 /* Convert the text describing the flags in a numeric value. */
132 flags_val = convert_flags (flags);
134 /* Something went wrong. */
137 /* Now run the actual test. */
140 #ifdef __UCLIBC_HAS_XLOCALE__
141 if (setlocale (LC_COLLATE, locale) == NULL
142 || setlocale (LC_CTYPE, locale) == NULL)
144 puts ("*** Cannot set locale");
149 /* skip non-ascii strings */
150 if (!str_isalpha(pattern) || !str_isalpha(input))
153 printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP multibyte test (requires locale support)\n", ++nr, pattern, input);
156 /* skip collating symbols */
157 if (str_has_funk(pattern, '.') || str_has_funk(input, '.'))
160 printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP collating symbol test (requires locale support)\n", ++nr, pattern, input);
163 /* skip equivalence class expressions */
164 if (str_has_funk(pattern, '=') || str_has_funk(input, '='))
167 printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP equivalence class test (requires locale support)\n", ++nr, pattern, input);
172 fnmres = fnmatch (pattern, input, flags_val);
174 printf ("%3d: fnmatch (\"%s\", \"%s\", %s) = %s%c",
176 escape (pattern, &escpatternlen, &escpattern),
177 escape (input, &escinputlen, &escinput),
178 flag_output (flags_val),
180 ? "0" : (fnmres == FNM_NOMATCH
182 : (sprintf (numbuf, "%d", fnmres), numbuf))),
183 (fnmres != 0) != (result != 0) ? ' ' : '\n');
185 if ((fnmres != 0) != (result != 0))
187 printf ("(FAIL, expected %s) ***\n",
189 ? "0" : (result == FNM_NOMATCH
191 : (sprintf (numbuf, "%d", result), numbuf)));
196 printf ("=====================\n%3d tests, %3d failed, %3d skipped\n", ntests, nfailed, nskipped);
207 next_input (char **line, int first, int last)
212 while (*cp == ' ' || *cp == '\t')
215 /* We allow comment lines starting with '#'. */
216 if (first && *cp == '#')
226 while (*cp != '"' && *cp != '\0' && *cp != '\n')
229 if (cp[1] == '\n' || cp[1] == '\0')
254 while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t')
257 if (cp == result && ! last)
258 /* Premature end of line. */
262 /* Terminate and skip over the next white spaces. */
271 convert_flags (const char *str)
279 if (strncasecmp (str, "PATHNAME", 8) == 0
280 && (str[8] == '|' || str[8] == '\0'))
282 result |= FNM_PATHNAME;
285 else if (strncasecmp (str, "NOESCAPE", 8) == 0
286 && (str[8] == '|' || str[8] == '\0'))
288 result |= FNM_NOESCAPE;
291 else if (strncasecmp (str, "PERIOD", 6) == 0
292 && (str[6] == '|' || str[6] == '\0'))
294 result |= FNM_PERIOD;
297 #ifdef FNM_LEADING_DIR
298 else if (strncasecmp (str, "LEADING_DIR", 11) == 0
299 && (str[11] == '|' || str[11] == '\0'))
301 result |= FNM_LEADING_DIR;
306 else if (strncasecmp (str, "CASEFOLD", 8) == 0
307 && (str[8] == '|' || str[8] == '\0'))
309 result |= FNM_CASEFOLD;
314 else if (strncasecmp (str, "EXTMATCH", 8) == 0
315 && (str[8] == '|' || str[8] == '\0'))
317 result |= FNM_EXTMATCH;
334 flag_output (int flags)
336 static char buf[100];
340 if (flags & FNM_PATHNAME)
342 cp = stpcpy (cp, "FNM_PATHNAME");
345 if (flags & FNM_NOESCAPE)
349 cp = stpcpy (cp, "FNM_NOESCAPE");
352 if (flags & FNM_PERIOD)
356 cp = stpcpy (cp, "FNM_PERIOD");
359 #ifdef FNM_LEADING_DIR
360 if (flags & FNM_LEADING_DIR)
364 cp = stpcpy (cp, "FNM_LEADING_DIR");
369 if (flags & FNM_CASEFOLD)
373 cp = stpcpy (cp, "FNM_CASEFOLD");
378 if (flags & FNM_EXTMATCH)
382 cp = stpcpy (cp, "FNM_EXTMATCH");
395 escape (const char *str, size_t *reslenp, char **resbufp)
397 size_t reslen = *reslenp;
398 char *resbuf = *resbufp;
399 size_t len = strlen (str);
402 if (2 * len + 1 > reslen)
404 resbuf = (char *) realloc (resbuf, 2 * len + 1);
406 error (EXIT_FAILURE, errno, "while allocating buffer for printing");
407 *reslenp = 2 * len + 1;
419 else if (*str == '\n')
425 else if (*str == '"')
431 else if (*str == '\\')