Imported Upstream version 1.8.1p2
[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 int
24 main(int argc, char *argv[])
25 {
26         FILE *fp = stdin;
27         char pattern[1024], string[1024], flagstr[1024];
28         int errors = 0, tests = 0, flags, got, want;
29
30         if (argc > 1) {
31                 if ((fp = fopen(argv[1], "r")) == NULL) {
32                         perror(argv[1]);
33                         exit(1);
34                 }
35         }
36
37         /*
38          * Read in test file, which is formatted thusly:
39          *
40          * pattern string flags expected_result
41          *
42          */
43         for (;;) {
44                 got = fscanf(fp, "%s %s %s %d\n", pattern, string, flagstr,
45                     &want);
46                 if (got == EOF)
47                         break;
48                 if (got == 4) {
49                         flags = 0;
50                         if (strcmp(flagstr, "FNM_NOESCAPE") == 0)
51                                 flags |= FNM_NOESCAPE;
52                         else if (strcmp(flagstr, "FNM_PATHNAME") == 0)
53                                 flags |= FNM_PATHNAME;
54                         else if (strcmp(flagstr, "FNM_PERIOD") == 0)
55                                 flags |= FNM_PERIOD;
56                         else if (strcmp(flagstr, "FNM_LEADING_DIR") == 0)
57                                 flags |= FNM_LEADING_DIR;
58                         else if (strcmp(flagstr, "FNM_CASEFOLD") == 0)
59                                 flags |= FNM_CASEFOLD;
60                         got = fnmatch(pattern, string, flags);
61                         if (got != want) {
62                                 fprintf(stderr,
63                                     "fnmatch: %s %s %d: want %d, got %d\n",
64                                     pattern, string, flags, want, got);
65                                 errors++;
66                         }
67                         tests++;
68                 }
69         }
70         if (tests != 0) {
71                 printf("fnmatch: %d test%s run, %d errors, %d%% success rate\n",
72                     tests, tests == 1 ? "" : "s", errors,
73                     (tests - errors) * 100 / tests);
74         }
75         exit(errors);
76 }