summaryrefslogtreecommitdiff
path: root/regress/lib/libc/glob/globtest.c
blob: e47a728da816d6949fccb0f7626feaef18502d40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*	$OpenBSD: globtest.c,v 1.2 2010/09/24 13:32:55 djm Exp $	*/

/*
 * Public domain, 2008, Todd C. Miller <Todd.Miller@courtesan.com>
 */

#include <err.h>
#include <glob.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_RESULTS	256

struct gl_entry {
	int flags;
	int nresults;
	char pattern[1024];
	char *results[MAX_RESULTS];
	long modes[MAX_RESULTS];
};

int test_glob(struct gl_entry *);

int
main(int argc, char **argv)
{
	FILE *fp = stdin;
	char *buf, *cp, *want, *got, *last;
	const char *errstr;
	int errors = 0, i, lineno, mode;
	struct gl_entry entry;
	size_t len;

	if (argc > 1) {
		if ((fp = fopen(argv[1], "r")) == NULL)
			err(1, "%s", argv[1]);
	}

	/*
	 * Read in test file, which is formatted thusly:
	 *
	 * [pattern] <flags>
	 * result1 [mode]
	 * result2 [mode]
	 * result3 [mode]
	 * ...
	 *
	 */
	lineno = 0;
	memset(&entry, 0, sizeof(entry));
	while ((buf = fgetln(fp, &len)) != NULL) {
		lineno++;
		if (buf[len - 1] != '\n')
			errx(1, "missing newline at EOF");
		buf[--len] = '\0';
		if (len == 0)
			continue; /* blank line */

		if (buf[0] == '[') {
			/* check previous pattern */
			if (entry.pattern[0])
				errors += test_glob(&entry);

			/* start new entry */
			if ((cp = strrchr(buf + 1, ']')) == NULL)
				errx(1, "invalid entry on line %d", lineno);
			len = cp - buf - 1;
			if (len >= sizeof(entry.pattern))
				errx(1, "pattern too big on line %d", lineno);
			memcpy(entry.pattern, buf + 1, len);
			entry.pattern[len] = '\0';

			buf = cp + 2;
			if (*buf++ != '<')
				errx(1, "invalid entry on line %d", lineno);
			if ((cp = strchr(buf, '>')) == NULL)
				errx(1, "invalid entry on line %d", lineno);
			entry.flags = (int)strtol(buf, &cp, 0);
			if (*cp != '>' || entry.flags < 0 || entry.flags > 0x4000)
				errx(1, "invalid flags: %s", buf);
			entry.nresults = 0;
			continue;
		}
		if (!entry.pattern[0])
			errx(1, "missing entry on line %d", lineno);

		if (entry.nresults + 1 > MAX_RESULTS) {
			errx(1, "too many results for %s, max %d",
			    entry.pattern, MAX_RESULTS);
		}
		if ((cp = strchr(buf, ' ')) != NULL) {
			*cp++ = '\0';
			mode = strtol(cp, NULL, 8);
		} else
			mode = -1;
		entry.modes[entry.nresults] = mode;
		entry.results[entry.nresults++] = strdup(buf);
	}
	if (entry.pattern[0])
		errors += test_glob(&entry); /* test last pattern */
	exit(errors);
}

int test_glob(struct gl_entry *entry)
{
	glob_t gl;
	int i;

	if (glob(entry->pattern, entry->flags, NULL, &gl) != 0)
		errx(1, "glob failed: %s", entry->pattern);

	if (gl.gl_matchc != entry->nresults)
		goto mismatch;

	for (i = 0; i < gl.gl_matchc; i++) {
		if (strcmp(gl.gl_pathv[i], entry->results[i]) != 0)
			goto mismatch;
		if ((entry->flags & GLOB_KEEPSTAT) != 0) {
			if (entry->modes[i] == -1 ||
			    gl.gl_statv[i] == NULL ||
			    entry->modes[i] != gl.gl_statv[i]->st_mode)
			goto badmode;
		}
		free(entry->results[i]);
	}
	return (0);
 badmode:
	warnx("mismatch mode for pattern %s, flags 0x%x, file \"%s\" "
	    "(found %07o, expected %07o)", entry->pattern, entry->flags,
	    gl.gl_pathv[i], gl.gl_statv[i] ? gl.gl_statv[i]->st_mode : 0,
	    entry->modes[i]);
	goto cleanup;
 mismatch:
	warnx("mismatch for pattern %s, flags 0x%x "
	    "(found \"%s\", expected \"%s\")", entry->pattern, entry->flags,
	    gl.gl_pathv[i], entry->results[i]);
 cleanup:
	while (i < gl.gl_matchc) {
		free(entry->results[i++]);
	}
	return (0);
	return (1);
}