xref: /openbsd/regress/lib/libc/fnmatch/fnm_test.c (revision 4cfece93)
1 /*	$OpenBSD: fnm_test.c,v 1.3 2019/01/25 00:19:26 millert Exp $	*/
2 
3 /*
4  * Public domain, 2008, Todd C. Miller <millert@openbsd.org>
5  */
6 
7 #include <err.h>
8 #include <fnmatch.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <util.h>
12 
13 int
14 main(int argc, char **argv)
15 {
16 	FILE *fp = stdin;
17 	char pattern[1024], string[1024];
18 	char *line;
19 	const char delim[3] = {'\0', '\0', '#'};
20 	int errors = 0, flags, got, want;
21 
22 	if (argc > 1) {
23 		if ((fp = fopen(argv[1], "r")) == NULL)
24 			err(1, "%s", argv[1]);
25 	}
26 
27 	/*
28 	 * Read in test file, which is formatted thusly:
29 	 *
30 	 * pattern string flags expected_result
31 	 *
32 	 * lines starting with '#' are comments
33 	 */
34 	for (;;) {
35 		line = fparseln(fp, NULL, NULL, delim, 0);
36 		if (!line)
37 			break;
38 		got = sscanf(line, "%s %s 0x%x %d", pattern, string, &flags,
39 		    &want);
40 		if (got == EOF) {
41 			free(line);
42 			break;
43 		}
44 		if (pattern[0] == '#') {
45 			free(line);
46 			continue;
47 		}
48 		if (got == 4) {
49 			got = fnmatch(pattern, string, flags);
50 			if (got != want) {
51 				warnx("%s %s %d: want %d, got %d", pattern,
52 				    string, flags, want, got);
53 				errors++;
54 			}
55 		} else {
56 			warnx("unrecognized line '%s'\n", line);
57 			errors++;
58 		}
59 		free(line);
60 	}
61 	exit(errors);
62 }
63