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"
26
#define MAX_RESULTS 256
32
char *results[MAX_RESULTS];
35
int test_glob(struct gl_entry *);
38
main(int argc, char **argv)
41
char buf[2048], *cp, *ep;
42
int errors = 0, tests = 0, lineno;
43
struct gl_entry entry;
47
if ((fp = fopen(argv[1], "r")) == NULL) {
54
* Read in test file, which is formatted thusly:
64
memset(&entry, 0, sizeof(entry));
65
while (fgets(buf, sizeof(buf), fp) != NULL) {
69
if (buf[len - 1] != '\n') {
71
"globtest: missing newline at EOF\n");
77
continue; /* blank line */
80
/* check previous pattern */
81
if (entry.pattern[0]) {
82
errors += test_glob(&entry);
87
if ((cp = strrchr(buf + 1, ']')) == NULL) {
89
"globtest: invalid entry on line %d\n",
94
if (len >= sizeof(entry.pattern)) {
96
"globtest: pattern too big on line %d\n",
100
memcpy(entry.pattern, buf + 1, len);
101
entry.pattern[len] = '\0';
106
"globtest: invalid entry on line %d\n",
110
ep = strchr(cp, '>');
113
"globtest: invalid entry on line %d\n",
119
for ((cp = strtok(cp, "|")); cp != NULL; (cp = strtok(NULL, "|"))) {
120
if (strcmp(cp, "GLOB_APPEND") == 0)
121
entry.flags |= GLOB_APPEND;
122
else if (strcmp(cp, "GLOB_DOOFFS") == 0)
123
entry.flags |= GLOB_DOOFFS;
124
else if (strcmp(cp, "GLOB_ERR") == 0)
125
entry.flags |= GLOB_ERR;
126
else if (strcmp(cp, "GLOB_MARK") == 0)
127
entry.flags |= GLOB_MARK;
128
else if (strcmp(cp, "GLOB_NOCHECK") == 0)
129
entry.flags |= GLOB_NOCHECK;
130
else if (strcmp(cp, "GLOB_NOSORT") == 0)
131
entry.flags |= GLOB_NOSORT;
132
else if (strcmp(cp, "GLOB_NOESCAPE") == 0)
133
entry.flags |= GLOB_NOESCAPE;
134
else if (strcmp(cp, "GLOB_BRACE") == 0)
135
entry.flags |= GLOB_BRACE;
136
else if (strcmp(cp, "GLOB_TILDE") == 0)
137
entry.flags |= GLOB_TILDE;
138
else if (strcmp(cp, "NONE") != 0) {
140
"globtest: invalid flags on line %d\n",
148
if (!entry.pattern[0]) {
149
fprintf(stderr, "globtest: missing entry on line %d\n",
154
if (entry.nresults + 1 > MAX_RESULTS) {
156
"globtest: too many results for %s, max %d\n",
157
entry.pattern, MAX_RESULTS);
160
entry.results[entry.nresults++] = strdup(buf);
162
if (entry.pattern[0]) {
163
errors += test_glob(&entry); /* test last pattern */
167
printf("glob: %d test%s run, %d errors, %d%% success rate\n",
168
tests, tests == 1 ? "" : "s", errors,
169
(tests - errors) * 100 / tests);
174
int test_glob(struct gl_entry *entry)
178
int nmatches = 0, i = 0;
180
if (glob(entry->pattern, entry->flags, NULL, &gl) != 0) {
181
fprintf(stderr, "glob failed: %s: %s\n", entry->pattern,
186
for (ap = gl.gl_pathv; *ap != NULL; ap++)
189
if (nmatches != entry->nresults)
192
for (i = 0; i < entry->nresults; i++) {
193
if (strcmp(gl.gl_pathv[i], entry->results[i]) != 0)
195
free(entry->results[i]);
199
if (nmatches != entry->nresults) {
201
"globtest: mismatch in number of results (found %d, expected %d) for pattern %s\n",
202
nmatches, entry->nresults, entry->pattern);
204
fprintf(stderr, "globtest: mismatch for pattern %s, flags 0x%x "
205
"(found \"%s\", expected \"%s\")\n", entry->pattern, entry->flags,
206
gl.gl_pathv[i], entry->results[i]);
207
while (i < entry->nresults)
208
free(entry->results[i++]);