xref: /openbsd-src/regress/lib/libc/fnmatch/fnm_test.c (revision bf198cc6eba0ca1f6d79f71e8e2243d386241fa8)
1*bf198cc6Smillert /*	$OpenBSD: fnm_test.c,v 1.3 2019/01/25 00:19:26 millert Exp $	*/
2b7855601Smillert 
3b7855601Smillert /*
4*bf198cc6Smillert  * Public domain, 2008, Todd C. Miller <millert@openbsd.org>
5b7855601Smillert  */
6b7855601Smillert 
7b7855601Smillert #include <err.h>
8b7855601Smillert #include <fnmatch.h>
9b7855601Smillert #include <stdio.h>
10b7855601Smillert #include <stdlib.h>
111cb26914Sstsp #include <util.h>
12b7855601Smillert 
13b7855601Smillert int
main(int argc,char ** argv)14b7855601Smillert main(int argc, char **argv)
15b7855601Smillert {
16b7855601Smillert 	FILE *fp = stdin;
17b7855601Smillert 	char pattern[1024], string[1024];
181cb26914Sstsp 	char *line;
191cb26914Sstsp 	const char delim[3] = {'\0', '\0', '#'};
20b7855601Smillert 	int errors = 0, flags, got, want;
21b7855601Smillert 
22b7855601Smillert 	if (argc > 1) {
23b7855601Smillert 		if ((fp = fopen(argv[1], "r")) == NULL)
24b7855601Smillert 			err(1, "%s", argv[1]);
25b7855601Smillert 	}
26b7855601Smillert 
27b7855601Smillert 	/*
28b7855601Smillert 	 * Read in test file, which is formatted thusly:
29b7855601Smillert 	 *
30b7855601Smillert 	 * pattern string flags expected_result
31b7855601Smillert 	 *
321cb26914Sstsp 	 * lines starting with '#' are comments
33b7855601Smillert 	 */
34b7855601Smillert 	for (;;) {
351cb26914Sstsp 		line = fparseln(fp, NULL, NULL, delim, 0);
361cb26914Sstsp 		if (!line)
37b7855601Smillert 			break;
381cb26914Sstsp 		got = sscanf(line, "%s %s 0x%x %d", pattern, string, &flags,
391cb26914Sstsp 		    &want);
401cb26914Sstsp 		if (got == EOF) {
411cb26914Sstsp 			free(line);
421cb26914Sstsp 			break;
431cb26914Sstsp 		}
441cb26914Sstsp 		if (pattern[0] == '#') {
451cb26914Sstsp 			free(line);
461cb26914Sstsp 			continue;
471cb26914Sstsp 		}
48b7855601Smillert 		if (got == 4) {
49b7855601Smillert 			got = fnmatch(pattern, string, flags);
50b7855601Smillert 			if (got != want) {
51b7855601Smillert 				warnx("%s %s %d: want %d, got %d", pattern,
52b7855601Smillert 				    string, flags, want, got);
53b7855601Smillert 				errors++;
54b7855601Smillert 			}
551cb26914Sstsp 		} else {
561cb26914Sstsp 			warnx("unrecognized line '%s'\n", line);
571cb26914Sstsp 			errors++;
58b7855601Smillert 		}
591cb26914Sstsp 		free(line);
60b7855601Smillert 	}
61b7855601Smillert 	exit(errors);
62b7855601Smillert }
63