summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/glob/globtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/regress/lib/libc/glob/globtest.c')
-rw-r--r--src/regress/lib/libc/glob/globtest.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/regress/lib/libc/glob/globtest.c b/src/regress/lib/libc/glob/globtest.c
new file mode 100644
index 0000000000..e47a728da8
--- /dev/null
+++ b/src/regress/lib/libc/glob/globtest.c
@@ -0,0 +1,144 @@
1/* $OpenBSD: globtest.c,v 1.2 2010/09/24 13:32:55 djm Exp $ */
2
3/*
4 * Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
5 */
6
7#include <err.h>
8#include <glob.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define MAX_RESULTS 256
14
15struct gl_entry {
16 int flags;
17 int nresults;
18 char pattern[1024];
19 char *results[MAX_RESULTS];
20 long modes[MAX_RESULTS];
21};
22
23int test_glob(struct gl_entry *);
24
25int
26main(int argc, char **argv)
27{
28 FILE *fp = stdin;
29 char *buf, *cp, *want, *got, *last;
30 const char *errstr;
31 int errors = 0, i, lineno, mode;
32 struct gl_entry entry;
33 size_t len;
34
35 if (argc > 1) {
36 if ((fp = fopen(argv[1], "r")) == NULL)
37 err(1, "%s", argv[1]);
38 }
39
40 /*
41 * Read in test file, which is formatted thusly:
42 *
43 * [pattern] <flags>
44 * result1 [mode]
45 * result2 [mode]
46 * result3 [mode]
47 * ...
48 *
49 */
50 lineno = 0;
51 memset(&entry, 0, sizeof(entry));
52 while ((buf = fgetln(fp, &len)) != NULL) {
53 lineno++;
54 if (buf[len - 1] != '\n')
55 errx(1, "missing newline at EOF");
56 buf[--len] = '\0';
57 if (len == 0)
58 continue; /* blank line */
59
60 if (buf[0] == '[') {
61 /* check previous pattern */
62 if (entry.pattern[0])
63 errors += test_glob(&entry);
64
65 /* start new entry */
66 if ((cp = strrchr(buf + 1, ']')) == NULL)
67 errx(1, "invalid entry on line %d", lineno);
68 len = cp - buf - 1;
69 if (len >= sizeof(entry.pattern))
70 errx(1, "pattern too big on line %d", lineno);
71 memcpy(entry.pattern, buf + 1, len);
72 entry.pattern[len] = '\0';
73
74 buf = cp + 2;
75 if (*buf++ != '<')
76 errx(1, "invalid entry on line %d", lineno);
77 if ((cp = strchr(buf, '>')) == NULL)
78 errx(1, "invalid entry on line %d", lineno);
79 entry.flags = (int)strtol(buf, &cp, 0);
80 if (*cp != '>' || entry.flags < 0 || entry.flags > 0x4000)
81 errx(1, "invalid flags: %s", buf);
82 entry.nresults = 0;
83 continue;
84 }
85 if (!entry.pattern[0])
86 errx(1, "missing entry on line %d", lineno);
87
88 if (entry.nresults + 1 > MAX_RESULTS) {
89 errx(1, "too many results for %s, max %d",
90 entry.pattern, MAX_RESULTS);
91 }
92 if ((cp = strchr(buf, ' ')) != NULL) {
93 *cp++ = '\0';
94 mode = strtol(cp, NULL, 8);
95 } else
96 mode = -1;
97 entry.modes[entry.nresults] = mode;
98 entry.results[entry.nresults++] = strdup(buf);
99 }
100 if (entry.pattern[0])
101 errors += test_glob(&entry); /* test last pattern */
102 exit(errors);
103}
104
105int test_glob(struct gl_entry *entry)
106{
107 glob_t gl;
108 int i;
109
110 if (glob(entry->pattern, entry->flags, NULL, &gl) != 0)
111 errx(1, "glob failed: %s", entry->pattern);
112
113 if (gl.gl_matchc != entry->nresults)
114 goto mismatch;
115
116 for (i = 0; i < gl.gl_matchc; i++) {
117 if (strcmp(gl.gl_pathv[i], entry->results[i]) != 0)
118 goto mismatch;
119 if ((entry->flags & GLOB_KEEPSTAT) != 0) {
120 if (entry->modes[i] == -1 ||
121 gl.gl_statv[i] == NULL ||
122 entry->modes[i] != gl.gl_statv[i]->st_mode)
123 goto badmode;
124 }
125 free(entry->results[i]);
126 }
127 return (0);
128 badmode:
129 warnx("mismatch mode for pattern %s, flags 0x%x, file \"%s\" "
130 "(found %07o, expected %07o)", entry->pattern, entry->flags,
131 gl.gl_pathv[i], gl.gl_statv[i] ? gl.gl_statv[i]->st_mode : 0,
132 entry->modes[i]);
133 goto cleanup;
134 mismatch:
135 warnx("mismatch for pattern %s, flags 0x%x "
136 "(found \"%s\", expected \"%s\")", entry->pattern, entry->flags,
137 gl.gl_pathv[i], entry->results[i]);
138 cleanup:
139 while (i < gl.gl_matchc) {
140 free(entry->results[i++]);
141 }
142 return (0);
143 return (1);
144}