xref: /openbsd-src/regress/lib/libc/fnmatch/fnm_test.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: fnm_test.c,v 1.1 2008/10/01 23:04:58 millert Exp $	*/
2 
3 /*
4  * Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
5  */
6 
7 #include <err.h>
8 #include <fnmatch.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 int
13 main(int argc, char **argv)
14 {
15 	FILE *fp = stdin;
16 	char pattern[1024], string[1024];
17 	int errors = 0, flags, got, want;
18 
19 	if (argc > 1) {
20 		if ((fp = fopen(argv[1], "r")) == NULL)
21 			err(1, "%s", argv[1]);
22 	}
23 
24 	/*
25 	 * Read in test file, which is formatted thusly:
26 	 *
27 	 * pattern string flags expected_result
28 	 *
29 	 */
30 	for (;;) {
31 		got = fscanf(fp, "%s %s 0x%x %d\n", pattern, string, &flags,
32 		    &want);
33 		if (got == EOF)
34 			break;
35 		if (got == 4) {
36 			got = fnmatch(pattern, string, flags);
37 			if (got != want) {
38 				warnx("%s %s %d: want %d, got %d", pattern,
39 				    string, flags, want, got);
40 				errors++;
41 			}
42 		}
43 	}
44 	exit(errors);
45 }
46