* stdio-common/vfscanf.c: Small cleanups throughout.
[glibc.git] / posix / tst-fnmatch.c
blob25471f8e413b77f7ea842e8c0d0956d679defe97
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
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <error.h>
22 #include <fnmatch.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
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
37 main (void)
39 char *linebuf = NULL;
40 size_t linebuflen = 0;
41 int ntests = 0;
42 int nfailed = 0;
43 char *escinput = NULL;
44 size_t escinputlen = 0;
45 char *escpattern = NULL;
46 size_t escpatternlen = 0;
47 int nr = 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);
58 char *cp;
59 const char *locale;
60 const char *input;
61 const char *pattern;
62 const char *result_str;
63 int result;
64 const char *flags;
65 int flags_val;
66 int fnmres;
67 char numbuf[24];
69 if (n == -1)
70 break;
72 if (n == 0)
73 /* Maybe an empty line. */
74 continue;
76 /* Skip over all leading white spaces. */
77 cp = linebuf;
79 locale = next_input (&cp, 1, 0);
80 if (locale == NULL)
81 continue;
83 input = next_input (&cp, 0, 0);
84 if (input == NULL)
85 continue;
87 pattern = next_input (&cp, 0, 0);
88 if (pattern == NULL)
89 continue;
91 result_str = next_input (&cp, 0, 0);
92 if (result_str == NULL)
93 continue;
95 if (strcmp (result_str, "0") == 0)
96 result = 0;
97 else if (strcasecmp (result_str, "NOMATCH") == 0)
98 result = FNM_NOMATCH;
99 else
101 char *endp;
102 result = strtol (result_str, &endp, 0);
103 if (*endp != '\0')
104 continue;
107 flags = next_input (&cp, 0, 1);
108 if (flags == NULL)
109 /* We allow the flags missing. */
110 flags = "";
112 /* Convert the text describing the flags in a numeric value. */
113 flags_val = convert_flags (flags);
114 if (flags_val == -1)
115 /* Something went wrong. */
116 continue;
118 /* Now run the actual test. */
119 ++ntests;
121 if (setlocale (LC_COLLATE, locale) == NULL
122 || setlocale (LC_CTYPE, locale) == NULL)
124 puts ("*** Cannot set locale");
125 ++nfailed;
126 continue;
129 fnmres = fnmatch (pattern, input, flags_val);
131 printf ("%3d: fnmatch (\"%s\", \"%s\", %s) = %s%c",
132 ++nr,
133 escape (pattern, &escpatternlen, &escpattern),
134 escape (input, &escinputlen, &escinput),
135 flag_output (flags_val),
136 (fnmres == 0
137 ? "0" : (fnmres == FNM_NOMATCH
138 ? "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",
145 result == 0
146 ? "0" : (result == FNM_NOMATCH
147 ? "FNM_NOMATCH"
148 : (sprintf (numbuf, "%d", result), numbuf)));
149 ++nfailed;
153 printf ("=====================\n%3d tests, %3d failed\n", ntests, nfailed);
155 free (escpattern);
156 free (escinput);
157 free (linebuf);
159 return nfailed != 0;
163 static char *
164 next_input (char **line, int first, int last)
166 char *cp = *line;
167 char *result;
169 while (*cp == ' ' || *cp == '\t')
170 ++cp;
172 /* We allow comment lines starting with '#'. */
173 if (first && *cp == '#')
174 return NULL;
176 if (*cp == '"')
178 char *wp;
180 result = ++cp;
181 wp = cp;
183 while (*cp != '"' && *cp != '\0' && *cp != '\n')
184 if (*cp == '\\')
186 if (cp[1] == '\n' || cp[1] == '\0')
187 return NULL;
189 ++cp;
190 if (*cp == 't')
191 *wp++ = '\t';
192 else if (*cp == 'n')
193 *wp++ = '\n';
194 else
195 *wp++ = *cp;
197 ++cp;
199 else
200 *wp++ = *cp++;
202 if (*cp != '"')
203 return NULL;
205 if (wp != cp)
206 *wp = '\0';
208 else
210 result = cp;
211 while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t')
212 ++cp;
214 if (cp == result && ! last)
215 /* Premature end of line. */
216 return NULL;
219 /* Terminate and skip over the next white spaces. */
220 *cp++ = '\0';
222 *line = cp;
223 return result;
227 static int
228 convert_flags (const char *str)
230 int result = 0;
232 while (*str != '\0')
234 int len;
236 if (strncasecmp (str, "PATHNAME", 8) == 0
237 && (str[8] == '|' || str[8] == '\0'))
239 result |= FNM_PATHNAME;
240 len = 8;
242 else if (strncasecmp (str, "NOESCAPE", 8) == 0
243 && (str[8] == '|' || str[8] == '\0'))
245 result |= FNM_NOESCAPE;
246 len = 8;
248 else if (strncasecmp (str, "PERIOD", 6) == 0
249 && (str[6] == '|' || str[6] == '\0'))
251 result |= FNM_PERIOD;
252 len = 6;
254 else if (strncasecmp (str, "LEADING_DIR", 11) == 0
255 && (str[11] == '|' || str[11] == '\0'))
257 result |= FNM_LEADING_DIR;
258 len = 11;
260 else if (strncasecmp (str, "CASEFOLD", 8) == 0
261 && (str[8] == '|' || str[8] == '\0'))
263 result |= FNM_CASEFOLD;
264 len = 8;
266 else if (strncasecmp (str, "EXTMATCH", 8) == 0
267 && (str[8] == '|' || str[8] == '\0'))
269 result |= FNM_EXTMATCH;
270 len = 8;
272 else
273 return -1;
275 str += len;
276 if (*str != '\0')
277 ++str;
280 return result;
284 static char *
285 flag_output (int flags)
287 static char buf[100];
288 int first = 1;
289 char *cp = buf;
291 if (flags & FNM_PATHNAME)
293 cp = stpcpy (cp, "FNM_PATHNAME");
294 first = 0;
296 if (flags & FNM_NOESCAPE)
298 if (! first)
299 *cp++ = '|';
300 cp = stpcpy (cp, "FNM_NOESCAPE");
301 first = 0;
303 if (flags & FNM_PERIOD)
305 if (! first)
306 *cp++ = '|';
307 cp = stpcpy (cp, "FNM_PERIOD");
308 first = 0;
310 if (flags & FNM_LEADING_DIR)
312 if (! first)
313 *cp++ = '|';
314 cp = stpcpy (cp, "FNM_LEADING_DIR");
315 first = 0;
317 if (flags & FNM_CASEFOLD)
319 if (! first)
320 *cp++ = '|';
321 cp = stpcpy (cp, "FNM_CASEFOLD");
322 first = 0;
324 if (flags & FNM_EXTMATCH)
326 if (! first)
327 *cp++ = '|';
328 cp = stpcpy (cp, "FNM_EXTMATCH");
329 first = 0;
331 if (cp == buf)
332 *cp++ = '0';
333 *cp = '\0';
335 return buf;
339 static char *
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);
345 char *wp;
347 if (2 * len + 1 > reslen)
349 resbuf = (char *) realloc (resbuf, 2 * len + 1);
350 if (resbuf == NULL)
351 error (EXIT_FAILURE, errno, "while allocating buffer for printing");
352 *reslenp = 2 * len + 1;
353 *resbufp = resbuf;
356 wp = resbuf;
357 while (*str != '\0')
358 if (*str == '\t')
360 *wp++ = '\\';
361 *wp++ = 't';
362 ++str;
364 else if (*str == '\n')
366 *wp++ = '\\';
367 *wp++ = 'n';
368 ++str;
370 else if (*str == '"')
372 *wp++ = '\\';
373 *wp++ = '"';
374 ++str;
376 else if (*str == '\\')
378 *wp++ = '\\';
379 *wp++ = '\\';
380 ++str;
382 else
383 *wp++ = *str++;
385 *wp = '\0';
387 return resbuf;