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 */
20 # include "compat/glob.h"
26 #define MAX_RESULTS 256
32 char *results[MAX_RESULTS];
35 int test_glob(struct gl_entry *);
36 __dso_public int main(int argc, char *argv[]);
39 main(int argc, char **argv)
42 char buf[2048], *cp, *ep;
43 int errors = 0, tests = 0, lineno;
44 struct gl_entry entry;
48 if ((fp = fopen(argv[1], "r")) == NULL) {
55 * Read in test file, which is formatted thusly:
65 memset(&entry, 0, sizeof(entry));
66 while (fgets(buf, sizeof(buf), fp) != NULL) {
70 if (buf[len - 1] != '\n') {
72 "globtest: missing newline at EOF\n");
78 continue; /* blank line */
81 /* check previous pattern */
82 if (entry.pattern[0]) {
83 errors += test_glob(&entry);
88 if ((cp = strrchr(buf + 1, ']')) == NULL) {
90 "globtest: invalid entry on line %d\n",
95 if (len >= sizeof(entry.pattern)) {
97 "globtest: pattern too big on line %d\n",
101 memcpy(entry.pattern, buf + 1, len);
102 entry.pattern[len] = '\0';
107 "globtest: invalid entry on line %d\n",
111 ep = strchr(cp, '>');
114 "globtest: invalid entry on line %d\n",
120 for ((cp = strtok(cp, "|")); cp != NULL; (cp = strtok(NULL, "|"))) {
121 if (strcmp(cp, "GLOB_APPEND") == 0)
122 entry.flags |= GLOB_APPEND;
123 else if (strcmp(cp, "GLOB_DOOFFS") == 0)
124 entry.flags |= GLOB_DOOFFS;
125 else if (strcmp(cp, "GLOB_ERR") == 0)
126 entry.flags |= GLOB_ERR;
127 else if (strcmp(cp, "GLOB_MARK") == 0)
128 entry.flags |= GLOB_MARK;
129 else if (strcmp(cp, "GLOB_NOCHECK") == 0)
130 entry.flags |= GLOB_NOCHECK;
131 else if (strcmp(cp, "GLOB_NOSORT") == 0)
132 entry.flags |= GLOB_NOSORT;
133 else if (strcmp(cp, "GLOB_NOESCAPE") == 0)
134 entry.flags |= GLOB_NOESCAPE;
135 else if (strcmp(cp, "GLOB_BRACE") == 0)
136 entry.flags |= GLOB_BRACE;
137 else if (strcmp(cp, "GLOB_TILDE") == 0)
138 entry.flags |= GLOB_TILDE;
139 else if (strcmp(cp, "NONE") != 0) {
141 "globtest: invalid flags on line %d\n",
149 if (!entry.pattern[0]) {
150 fprintf(stderr, "globtest: missing entry on line %d\n",
155 if (entry.nresults + 1 > MAX_RESULTS) {
157 "globtest: too many results for %s, max %d\n",
158 entry.pattern, MAX_RESULTS);
161 entry.results[entry.nresults++] = strdup(buf);
163 if (entry.pattern[0]) {
164 errors += test_glob(&entry); /* test last pattern */
168 printf("glob: %d test%s run, %d errors, %d%% success rate\n",
169 tests, tests == 1 ? "" : "s", errors,
170 (tests - errors) * 100 / tests);
175 int test_glob(struct gl_entry *entry)
179 int nmatches = 0, i = 0;
181 if (glob(entry->pattern, entry->flags, NULL, &gl) != 0) {
182 fprintf(stderr, "glob failed: %s: %s\n", entry->pattern,
187 for (ap = gl.gl_pathv; *ap != NULL; ap++)
190 if (nmatches != entry->nresults)
193 for (i = 0; i < entry->nresults; i++) {
194 if (strcmp(gl.gl_pathv[i], entry->results[i]) != 0)
196 free(entry->results[i]);
200 if (nmatches != entry->nresults) {
202 "globtest: mismatch in number of results (found %d, expected %d) for pattern %s\n",
203 nmatches, entry->nresults, entry->pattern);
205 fprintf(stderr, "globtest: mismatch for pattern %s, flags 0x%x "
206 "(found \"%s\", expected \"%s\")\n", entry->pattern, entry->flags,
207 gl.gl_pathv[i], entry->results[i]);
208 while (i < entry->nresults)
209 free(entry->results[i++]);