1 /* $OpenBSD: globtest.c,v 1.1 2008/10/01 23:04:36 millert Exp $ */
4 * Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
13 #endif /* HAVE_STRING_H */
16 #endif /* HAVE_STRINGS_H */
17 #ifdef HAVE_EXTENDED_GLOB
20 # include "compat/glob.h"
24 #define MAX_RESULTS 256
34 char *results[MAX_RESULTS];
37 int test_glob(struct gl_entry *);
40 main(int argc, char **argv)
43 char buf[2048], *cp, *ep;
44 int errors = 0, tests = 0, lineno;
45 struct gl_entry entry;
49 if ((fp = fopen(argv[1], "r")) == NULL) {
56 * Read in test file, which is formatted thusly:
66 memset(&entry, 0, sizeof(entry));
67 while (fgets(buf, sizeof(buf), fp) != NULL) {
71 if (buf[len - 1] != '\n') {
73 "globtest: missing newline at EOF\n");
79 continue; /* blank line */
82 /* check previous pattern */
83 if (entry.pattern[0]) {
84 errors += test_glob(&entry);
89 if ((cp = strrchr(buf + 1, ']')) == NULL) {
91 "globtest: invalid entry on line %d\n",
96 if (len >= sizeof(entry.pattern)) {
98 "globtest: pattern too big on line %d\n",
102 memcpy(entry.pattern, buf + 1, len);
103 entry.pattern[len] = '\0';
108 "globtest: invalid entry on line %d\n",
112 ep = strchr(cp, '>');
115 "globtest: invalid entry on line %d\n",
121 for ((cp = strtok(cp, "|")); cp != NULL; (cp = strtok(NULL, "|"))) {
122 if (strcmp(cp, "GLOB_APPEND") == 0)
123 entry.flags |= GLOB_APPEND;
124 else if (strcmp(cp, "GLOB_DOOFFS") == 0)
125 entry.flags |= GLOB_DOOFFS;
126 else if (strcmp(cp, "GLOB_ERR") == 0)
127 entry.flags |= GLOB_ERR;
128 else if (strcmp(cp, "GLOB_MARK") == 0)
129 entry.flags |= GLOB_MARK;
130 else if (strcmp(cp, "GLOB_NOCHECK") == 0)
131 entry.flags |= GLOB_NOCHECK;
132 else if (strcmp(cp, "GLOB_NOSORT") == 0)
133 entry.flags |= GLOB_NOSORT;
134 else if (strcmp(cp, "GLOB_NOESCAPE") == 0)
135 entry.flags |= GLOB_NOESCAPE;
136 else if (strcmp(cp, "GLOB_BRACE") == 0)
137 entry.flags |= GLOB_BRACE;
138 else if (strcmp(cp, "GLOB_TILDE") == 0)
139 entry.flags |= GLOB_TILDE;
140 else if (strcmp(cp, "NONE") != 0) {
142 "globtest: invalid flags on line %d\n",
150 if (!entry.pattern[0]) {
151 fprintf(stderr, "globtest: missing entry on line %d\n",
156 if (entry.nresults + 1 > MAX_RESULTS) {
158 "globtest: too many results for %s, max %d\n",
159 entry.pattern, MAX_RESULTS);
162 entry.results[entry.nresults++] = strdup(buf);
164 if (entry.pattern[0]) {
165 errors += test_glob(&entry); /* test last pattern */
169 printf("glob: %d test%s run, %d errors, %d%% success rate\n",
170 tests, tests == 1 ? "" : "s", errors,
171 (tests - errors) * 100 / tests);
176 int test_glob(struct gl_entry *entry)
180 int nmatches = 0, i = 0;
182 if (glob(entry->pattern, entry->flags, NULL, &gl) != 0) {
183 fprintf(stderr, "glob failed: %s: %s\n", entry->pattern,
188 for (ap = gl.gl_pathv; *ap != NULL; ap++)
191 if (nmatches != entry->nresults)
194 for (i = 0; i < entry->nresults; i++) {
195 if (strcmp(gl.gl_pathv[i], entry->results[i]) != 0)
197 free(entry->results[i]);
201 if (nmatches != entry->nresults) {
203 "globtest: mismatch in number of results (found %d, expected %d) for pattern %s\n",
204 nmatches, entry->nresults, entry->pattern);
206 fprintf(stderr, "globtest: mismatch for pattern %s, flags 0x%x "
207 "(found \"%s\", expected \"%s\")\n", entry->pattern, entry->flags,
208 gl.gl_pathv[i], entry->results[i]);
209 while (i < entry->nresults)
210 free(entry->results[i++]);