If there is no lchown(), don't try to set the user & group of a symlink.
[rsync/rsync.git] / wildtest.c
CommitLineData
5de45bca
WD
1/*
2** wildmatch test suite.
3*/
4
6cd50960 5/*#define COMPARE_WITH_FNMATCH*/
5de45bca 6
d5c973cc 7#define WILD_TEST_ITERATIONS
076f60ee
WD
8#include "lib/wildmatch.c"
9
10#include "popt.h"
11
5de45bca
WD
12#ifdef COMPARE_WITH_FNMATCH
13#include <fnmatch.h>
97d53f8c
WD
14
15int fnmatch_errors = 0;
5de45bca
WD
16#endif
17
97d53f8c
WD
18int wildmatch_errors = 0;
19
5de45bca
WD
20typedef char bool;
21
d5c973cc 22int output_iterations = 0;
076f60ee
WD
23
24static struct poptOption long_options[] = {
25 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
9a17dddb 26 {"iterations", 'i', POPT_ARG_NONE, &output_iterations, 0, 0, 0},
076f60ee
WD
27 {0,0,0,0, 0, 0, 0}
28};
29
5de45bca
WD
30/* match just at the start of string (anchored tests) */
31static void
97d53f8c
WD
32run_test(int line, bool matches, bool same_as_fnmatch,
33 const char *text, const char *pattern)
5de45bca
WD
34{
35 bool matched;
36#ifdef COMPARE_WITH_FNMATCH
37 bool fn_matched;
38 int flags = strstr(pattern, "**")? 0 : FNM_PATHNAME;
88085892
WD
39#else
40 same_as_fnmatch = 0; /* Get rid of unused-variable compiler warning. */
5de45bca
WD
41#endif
42
43 matched = wildmatch(pattern, text);
44#ifdef COMPARE_WITH_FNMATCH
45 fn_matched = !fnmatch(pattern, text, flags);
46#endif
47 if (matched != matches) {
97d53f8c
WD
48 printf("wildmatch failure on line %d:\n %s\n %s\n expected %s match\n",
49 line, text, pattern, matches? "a" : "NO");
50 wildmatch_errors++;
5de45bca
WD
51 }
52#ifdef COMPARE_WITH_FNMATCH
53 if (fn_matched != (matches ^ !same_as_fnmatch)) {
97d53f8c
WD
54 printf("fnmatch disagreement on line %d:\n %s\n %s\n expected %s match\n",
55 line, text, pattern, matches ^ !same_as_fnmatch? "a" : "NO");
56 fnmatch_errors++;
5de45bca
WD
57 }
58#endif
b6b42c89
WD
59 if (output_iterations) {
60 printf("%d: \"%s\" iterations = %d\n", line, pattern,
61 wildmatch_iteration_count);
62 }
5de45bca
WD
63}
64
65int
66main(int argc, char **argv)
67{
97d53f8c
WD
68 char buf[2048], *s, *string[2], *end[2];
69 FILE *fp;
70 int opt, line, i, flag[2];
076f60ee
WD
71 poptContext pc = poptGetContext("wildtest", argc, (const char**)argv,
72 long_options, 0);
73
74 while ((opt = poptGetNextOpt(pc)) != -1) {
75 switch (opt) {
76 default:
9a17dddb
WD
77 fprintf(stderr, "%s: %s\n",
78 poptBadOption(pc, POPT_BADOPTION_NOALIAS),
79 poptStrerror(opt));
076f60ee
WD
80 exit(1);
81 }
82 }
5de45bca 83
c4cd2ca6
WD
84 argv = (char**)poptGetArgs(pc);
85 if (!argv || argv[1]) {
86 fprintf(stderr, "Usage: wildtest TESTFILE\n");
87 exit(1);
88 }
89
90 if ((fp = fopen(*argv, "r")) == NULL) {
91 fprintf(stderr, "Unable to open %s\n", *argv);
97d53f8c
WD
92 exit(1);
93 }
94
95 line = 0;
96 while (fgets(buf, sizeof buf, fp)) {
97 line++;
98 if (*buf == '#' || *buf == '\n')
99 continue;
100 for (s = buf, i = 0; i <= 1; i++) {
101 if (*s == '1')
102 flag[i] = 1;
103 else if (*s == '0')
104 flag[i] = 0;
105 else
106 flag[i] = -1;
107 if (*++s != ' ' && *s != '\t')
108 flag[i] = -1;
109 if (flag[i] < 0) {
c4cd2ca6
WD
110 fprintf(stderr, "Invalid flag syntax on line %d of %s:\n%s",
111 line, *argv, buf);
97d53f8c
WD
112 exit(1);
113 }
114 while (*++s == ' ' || *s == '\t') {}
115 }
116 for (i = 0; i <= 1; i++) {
117 if (*s == '\'' || *s == '"' || *s == '`') {
118 char quote = *s++;
119 string[i] = s;
120 while (*s && *s != quote) s++;
121 if (!*s) {
c4cd2ca6
WD
122 fprintf(stderr, "Unmatched quote on line %d of %s:\n%s",
123 line, *argv, buf);
97d53f8c
WD
124 exit(1);
125 }
126 end[i] = s;
127 }
128 else {
129 if (!*s || *s == '\n') {
c4cd2ca6
WD
130 fprintf(stderr, "Not enough strings on line %d of %s:\n%s",
131 line, *argv, buf);
97d53f8c
WD
132 exit(1);
133 }
134 string[i] = s;
135 while (*++s && *s != ' ' && *s != '\t' && *s != '\n') {}
136 end[i] = s;
137 }
138 while (*++s == ' ' || *s == '\t') {}
139 }
140 *end[0] = *end[1] = '\0';
141 run_test(line, flag[0], flag[1], string[0], string[1]);
142 }
143
144 if (!wildmatch_errors)
16859cdb 145 fputs("No", stdout);
97d53f8c 146 else
16859cdb
WD
147 printf("%d", wildmatch_errors);
148 printf(" wildmatch error%s found.\n", wildmatch_errors == 1? "" : "s");
97d53f8c
WD
149
150#ifdef COMPARE_WITH_FNMATCH
151 if (!fnmatch_errors)
16859cdb 152 fputs("No", stdout);
97d53f8c 153 else
16859cdb
WD
154 printf("%d", fnmatch_errors);
155 printf(" fnmatch error%s found.\n", fnmatch_errors == 1? "" : "s");
97d53f8c
WD
156
157#endif
5de45bca
WD
158
159 return 0;
160}