t_glob.c revision 302408
1/*	$NetBSD: t_glob.c,v 1.3 2013/01/02 11:28:48 martin Exp $	*/
2/*-
3 * Copyright (c) 2010 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Christos Zoulas
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in
17 *    the documentation and/or other materials provided with the
18 *    distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__RCSID("$NetBSD: t_glob.c,v 1.3 2013/01/02 11:28:48 martin Exp $");
36
37#include <atf-c.h>
38
39#include <sys/param.h>
40#include <sys/stat.h>
41
42#include <dirent.h>
43#include <glob.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <errno.h>
48
49#ifdef __FreeBSD__
50#include "h_macros.h"
51#define	__gl_stat_t struct stat
52#define	_S_IFDIR S_IFDIR
53#else
54#include "../../../h_macros.h"
55#endif
56
57
58#ifdef DEBUG
59#define DPRINTF(a) printf a
60#else
61#define DPRINTF(a)
62#endif
63
64struct gl_file {
65	const char *name;
66	int dir;
67};
68
69static struct gl_file a[] = {
70	{ "1", 0 },
71	{ "b", 1 },
72	{ "3", 0 },
73	{ "4", 0 },
74};
75
76static struct gl_file b[] = {
77	{ "x", 0 },
78	{ "y", 0 },
79	{ "z", 0 },
80	{ "w", 0 },
81};
82
83struct gl_dir {
84	const char *name;	/* directory name */
85	const struct gl_file *dir;
86	size_t len, pos;
87};
88
89static struct gl_dir d[] = {
90	{ "a", a, __arraycount(a), 0 },
91	{ "a/b", b, __arraycount(b), 0 },
92};
93
94#ifndef __FreeBSD__
95static const char *glob_star[] = {
96    "a/1", "a/3", "a/4", "a/b", "a/b/w", "a/b/x", "a/b/y", "a/b/z",
97};
98#endif
99
100static const char *glob_star_not[] = {
101	"a/1", "a/3", "a/4", "a/b",
102};
103
104static void
105trim(char *buf, size_t len, const char *name)
106{
107	char *path = buf, *epath = buf + len;
108	while (path < epath && (*path++ = *name++) != '\0')
109		continue;
110	path--;
111	while (path > buf && *--path == '/')
112		*path = '\0';
113}
114
115static void *
116gl_opendir(const char *dir)
117{
118	size_t i;
119	char buf[MAXPATHLEN];
120	trim(buf, sizeof(buf), dir);
121
122	for (i = 0; i < __arraycount(d); i++)
123		if (strcmp(buf, d[i].name) == 0) {
124			DPRINTF(("opendir %s %zu\n", buf, i));
125			return &d[i];
126		}
127	errno = ENOENT;
128	return NULL;
129}
130
131static struct dirent *
132gl_readdir(void *v)
133{
134	static struct dirent dir;
135	struct gl_dir *dd = v;
136	if (dd->pos < dd->len) {
137		const struct gl_file *f = &dd->dir[dd->pos++];
138		strcpy(dir.d_name, f->name);
139		dir.d_namlen = strlen(f->name);
140		dir.d_ino = dd->pos;
141		dir.d_type = f->dir ? DT_DIR : DT_REG;
142		DPRINTF(("readdir %s %d\n", dir.d_name, dir.d_type));
143#ifdef __FreeBSD__
144		dir.d_reclen = -1; /* Does not have _DIRENT_RECLEN */
145#else
146		dir.d_reclen = _DIRENT_RECLEN(&dir, dir.d_namlen);
147#endif
148		return &dir;
149	}
150	return NULL;
151}
152
153static int
154gl_stat(const char *name , __gl_stat_t *st)
155{
156	char buf[MAXPATHLEN];
157	trim(buf, sizeof(buf), name);
158	memset(st, 0, sizeof(*st));
159
160	if (strcmp(buf, "a") == 0 || strcmp(buf, "a/b") == 0) {
161		st->st_mode |= _S_IFDIR;
162		return 0;
163	}
164
165	if (buf[0] == 'a' && buf[1] == '/') {
166		struct gl_file *f;
167		size_t offs, count;
168
169		if (buf[2] == 'b' && buf[3] == '/') {
170			offs = 4;
171			count = __arraycount(b);
172			f = b;
173		} else {
174			offs = 2;
175			count = __arraycount(a);
176			f = a;
177		}
178
179		for (size_t i = 0; i < count; i++)
180			if (strcmp(f[i].name, buf + offs) == 0)
181				return 0;
182	}
183	DPRINTF(("stat %s %d\n", buf, st->st_mode));
184	errno = ENOENT;
185	return -1;
186}
187
188static int
189gl_lstat(const char *name , __gl_stat_t *st)
190{
191	return gl_stat(name, st);
192}
193
194static void
195gl_closedir(void *v)
196{
197	struct gl_dir *dd = v;
198	dd->pos = 0;
199	DPRINTF(("closedir %p\n", dd));
200}
201
202static void
203run(const char *p, int flags, const char **res, size_t len)
204{
205	glob_t gl;
206	size_t i;
207
208	memset(&gl, 0, sizeof(gl));
209	gl.gl_opendir = gl_opendir;
210	gl.gl_readdir = gl_readdir;
211	gl.gl_closedir = gl_closedir;
212	gl.gl_stat = gl_stat;
213	gl.gl_lstat = gl_lstat;
214
215	RZ(glob(p, GLOB_ALTDIRFUNC | flags, NULL, &gl));
216
217	for (i = 0; i < gl.gl_pathc; i++)
218		DPRINTF(("%s\n", gl.gl_pathv[i]));
219
220	ATF_CHECK(len == gl.gl_pathc);
221	for (i = 0; i < gl.gl_pathc; i++)
222		ATF_CHECK_STREQ(gl.gl_pathv[i], res[i]);
223
224	globfree(&gl);
225}
226
227
228#ifndef __FreeBSD__
229ATF_TC(glob_star);
230ATF_TC_HEAD(glob_star, tc)
231{
232	atf_tc_set_md_var(tc, "descr",
233	    "Test glob(3) ** with GLOB_STAR");
234}
235
236ATF_TC_BODY(glob_star, tc)
237{
238	run("a/**", GLOB_STAR, glob_star, __arraycount(glob_star));
239}
240#endif
241
242ATF_TC(glob_star_not);
243ATF_TC_HEAD(glob_star_not, tc)
244{
245	atf_tc_set_md_var(tc, "descr",
246	    "Test glob(3) ** without GLOB_STAR");
247}
248
249
250ATF_TC_BODY(glob_star_not, tc)
251{
252	run("a/**", 0, glob_star_not, __arraycount(glob_star_not));
253}
254
255#if 0
256ATF_TC(glob_nocheck);
257ATF_TC_HEAD(glob_nocheck, tc)
258{
259	atf_tc_set_md_var(tc, "descr",
260	    "Test glob(3) pattern with backslash and GLOB_NOCHECK");
261}
262
263
264ATF_TC_BODY(glob_nocheck, tc)
265{
266	static const char pattern[] = { 'f', 'o', 'o', '\\', ';', 'b', 'a',
267	    'r', '\0' };
268	static const char *glob_nocheck[] = {
269	    pattern
270	};
271	run(pattern, GLOB_NOCHECK, glob_nocheck, __arraycount(glob_nocheck));
272}
273#endif
274
275ATF_TP_ADD_TCS(tp)
276{
277#ifndef __FreeBSD__
278	ATF_TP_ADD_TC(tp, glob_star);
279#endif
280	ATF_TP_ADD_TC(tp, glob_star_not);
281/*
282 * Remove this test for now - the GLOB_NOCHECK return value has been
283 * re-defined to return a modified pattern in revision 1.33 of glob.c
284 *
285 *	ATF_TP_ADD_TC(tp, glob_nocheck);
286 */
287
288	return atf_no_error();
289}
290