Imported Upstream version 1.8.7
[debian/sudo] / compat / regress / fnmatch / fnm_test.c
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 <config.h>
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #ifdef HAVE_STRING_H
12 # include <string.h>
13 #endif /* HAVE_STRING_H */
14 #ifdef HAVE_STRINGS_H
15 # include <strings.h>
16 #endif /* HAVE_STRINGS_H */
17 #ifdef HAVE_FNMATCH
18 # include <fnmatch.h>
19 #else
20 # include "compat/fnmatch.h"
21 #endif
22
23 #include "missing.h"
24
25 __dso_public int main(int argc, char *argv[]);
26
27 int
28 main(int argc, char *argv[])
29 {
30         FILE *fp = stdin;
31         char pattern[1024], string[1024], flagstr[1024];
32         int errors = 0, tests = 0, flags, got, want;
33
34         if (argc > 1) {
35                 if ((fp = fopen(argv[1], "r")) == NULL) {
36                         perror(argv[1]);
37                         exit(1);
38                 }
39         }
40
41         /*
42          * Read in test file, which is formatted thusly:
43          *
44          * pattern string flags expected_result
45          *
46          */
47         for (;;) {
48                 got = fscanf(fp, "%s %s %s %d\n", pattern, string, flagstr,
49                     &want);
50                 if (got == EOF)
51                         break;
52                 if (got == 4) {
53                         flags = 0;
54                         if (strcmp(flagstr, "FNM_NOESCAPE") == 0)
55                                 flags |= FNM_NOESCAPE;
56                         else if (strcmp(flagstr, "FNM_PATHNAME") == 0)
57                                 flags |= FNM_PATHNAME;
58                         else if (strcmp(flagstr, "FNM_PERIOD") == 0)
59                                 flags |= FNM_PERIOD;
60                         else if (strcmp(flagstr, "FNM_LEADING_DIR") == 0)
61                                 flags |= FNM_LEADING_DIR;
62                         else if (strcmp(flagstr, "FNM_CASEFOLD") == 0)
63                                 flags |= FNM_CASEFOLD;
64                         got = fnmatch(pattern, string, flags);
65                         if (got != want) {
66                                 fprintf(stderr,
67                                     "fnmatch: %s %s %d: want %d, got %d\n",
68                                     pattern, string, flags, want, got);
69                                 errors++;
70                         }
71                         tests++;
72                 }
73         }
74         if (tests != 0) {
75                 printf("fnmatch: %d test%s run, %d errors, %d%% success rate\n",
76                     tests, tests == 1 ? "" : "s", errors,
77                     (tests - errors) * 100 / tests);
78         }
79         exit(errors);
80 }