xref: /freebsd/contrib/netbsd-tests/lib/libc/gen/t_glob.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
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 
64 struct gl_file {
65 	const char *name;
66 	int dir;
67 };
68 
69 static struct gl_file a[] = {
70 	{ "1", 0 },
71 	{ "b", 1 },
72 	{ "3", 0 },
73 	{ "4", 0 },
74 };
75 
76 static struct gl_file b[] = {
77 	{ "x", 0 },
78 	{ "y", 0 },
79 	{ "z", 0 },
80 	{ "w", 0 },
81 };
82 
83 struct gl_dir {
84 	const char *name;	/* directory name */
85 	const struct gl_file *dir;
86 	size_t len, pos;
87 };
88 
89 static struct gl_dir d[] = {
90 	{ "a", a, __arraycount(a), 0 },
91 	{ "a/b", b, __arraycount(b), 0 },
92 };
93 
94 static const char *glob_star[] = {
95     "a/1", "a/3", "a/4", "a/b", "a/b/w", "a/b/x", "a/b/y", "a/b/z",
96 };
97 
98 static const char *glob_star_not[] = {
99 	"a/1", "a/3", "a/4", "a/b",
100 };
101 
102 static void
103 trim(char *buf, size_t len, const char *name)
104 {
105 	char *path = buf, *epath = buf + len;
106 	while (path < epath && (*path++ = *name++) != '\0')
107 		continue;
108 	path--;
109 	while (path > buf && *--path == '/')
110 		*path = '\0';
111 }
112 
113 static void *
114 gl_opendir(const char *dir)
115 {
116 	size_t i;
117 	char buf[MAXPATHLEN];
118 	trim(buf, sizeof(buf), dir);
119 
120 	for (i = 0; i < __arraycount(d); i++)
121 		if (strcmp(buf, d[i].name) == 0) {
122 			DPRINTF(("opendir %s %zu\n", buf, i));
123 			return &d[i];
124 		}
125 	errno = ENOENT;
126 	return NULL;
127 }
128 
129 static struct dirent *
130 gl_readdir(void *v)
131 {
132 	static struct dirent dir;
133 	struct gl_dir *dd = v;
134 	if (dd->pos < dd->len) {
135 		const struct gl_file *f = &dd->dir[dd->pos++];
136 		strcpy(dir.d_name, f->name);
137 		dir.d_namlen = strlen(f->name);
138 		dir.d_ino = dd->pos;
139 		dir.d_type = f->dir ? DT_DIR : DT_REG;
140 		DPRINTF(("readdir %s %d\n", dir.d_name, dir.d_type));
141 #ifdef __FreeBSD__
142 		dir.d_reclen = -1; /* Does not have _DIRENT_RECLEN */
143 #else
144 		dir.d_reclen = _DIRENT_RECLEN(&dir, dir.d_namlen);
145 #endif
146 		return &dir;
147 	}
148 	return NULL;
149 }
150 
151 static int
152 gl_stat(const char *name , __gl_stat_t *st)
153 {
154 	char buf[MAXPATHLEN];
155 	trim(buf, sizeof(buf), name);
156 	memset(st, 0, sizeof(*st));
157 
158 	if (strcmp(buf, "a") == 0 || strcmp(buf, "a/b") == 0) {
159 		st->st_mode |= _S_IFDIR;
160 		return 0;
161 	}
162 
163 	if (buf[0] == 'a' && buf[1] == '/') {
164 		struct gl_file *f;
165 		size_t offs, count;
166 
167 		if (buf[2] == 'b' && buf[3] == '/') {
168 			offs = 4;
169 			count = __arraycount(b);
170 			f = b;
171 		} else {
172 			offs = 2;
173 			count = __arraycount(a);
174 			f = a;
175 		}
176 
177 		for (size_t i = 0; i < count; i++)
178 			if (strcmp(f[i].name, buf + offs) == 0)
179 				return 0;
180 	}
181 	DPRINTF(("stat %s %d\n", buf, st->st_mode));
182 	errno = ENOENT;
183 	return -1;
184 }
185 
186 static int
187 gl_lstat(const char *name , __gl_stat_t *st)
188 {
189 	return gl_stat(name, st);
190 }
191 
192 static void
193 gl_closedir(void *v)
194 {
195 	struct gl_dir *dd = v;
196 	dd->pos = 0;
197 	DPRINTF(("closedir %p\n", dd));
198 }
199 
200 static void
201 run(const char *p, int flags, const char **res, size_t len)
202 {
203 	glob_t gl;
204 	size_t i;
205 
206 	memset(&gl, 0, sizeof(gl));
207 	gl.gl_opendir = gl_opendir;
208 	gl.gl_readdir = gl_readdir;
209 	gl.gl_closedir = gl_closedir;
210 	gl.gl_stat = gl_stat;
211 	gl.gl_lstat = gl_lstat;
212 
213 	RZ(glob(p, GLOB_ALTDIRFUNC | flags, NULL, &gl));
214 
215 	for (i = 0; i < gl.gl_pathc; i++)
216 		DPRINTF(("%s\n", gl.gl_pathv[i]));
217 
218 	ATF_CHECK(len == gl.gl_pathc);
219 	for (i = 0; i < gl.gl_pathc; i++)
220 		ATF_CHECK_STREQ(gl.gl_pathv[i], res[i]);
221 
222 	globfree(&gl);
223 }
224 
225 
226 #ifndef __FreeBSD__
227 ATF_TC(glob_star);
228 ATF_TC_HEAD(glob_star, tc)
229 {
230 	atf_tc_set_md_var(tc, "descr",
231 	    "Test glob(3) ** with GLOB_STAR");
232 }
233 
234 ATF_TC_BODY(glob_star, tc)
235 {
236 	run("a/**", GLOB_STAR, glob_star, __arraycount(glob_star));
237 }
238 #endif
239 
240 ATF_TC(glob_star_not);
241 ATF_TC_HEAD(glob_star_not, tc)
242 {
243 	atf_tc_set_md_var(tc, "descr",
244 	    "Test glob(3) ** without GLOB_STAR");
245 }
246 
247 
248 ATF_TC_BODY(glob_star_not, tc)
249 {
250 	run("a/**", 0, glob_star_not, __arraycount(glob_star_not));
251 }
252 
253 #if 0
254 ATF_TC(glob_nocheck);
255 ATF_TC_HEAD(glob_nocheck, tc)
256 {
257 	atf_tc_set_md_var(tc, "descr",
258 	    "Test glob(3) pattern with backslash and GLOB_NOCHECK");
259 }
260 
261 
262 ATF_TC_BODY(glob_nocheck, tc)
263 {
264 	static const char pattern[] = { 'f', 'o', 'o', '\\', ';', 'b', 'a',
265 	    'r', '\0' };
266 	static const char *glob_nocheck[] = {
267 	    pattern
268 	};
269 	run(pattern, GLOB_NOCHECK, glob_nocheck, __arraycount(glob_nocheck));
270 }
271 #endif
272 
273 ATF_TP_ADD_TCS(tp)
274 {
275 #ifndef __FreeBSD__
276 	ATF_TP_ADD_TC(tp, glob_star);
277 #endif
278 	ATF_TP_ADD_TC(tp, glob_star_not);
279 /*
280  * Remove this test for now - the GLOB_NOCHECK return value has been
281  * re-defined to return a modified pattern in revision 1.33 of glob.c
282  *
283  *	ATF_TP_ADD_TC(tp, glob_nocheck);
284  */
285 
286 	return atf_no_error();
287 }
288