1 /* $NetBSD: t_glob.c,v 1.3 2013/01/02 11:28:48 martin Exp $ */
3 * Copyright (c) 2010 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_glob.c,v 1.3 2013/01/02 11:28:48 martin Exp $");
39 #include <sys/param.h>
51 #define __gl_stat_t struct stat
52 #define _S_IFDIR S_IFDIR
54 #include "../../../h_macros.h"
59 #define DPRINTF(a) printf a
69 static struct gl_file a[] = {
76 static struct gl_file b[] = {
84 const char *name; /* directory name */
85 const struct gl_file *dir;
89 static struct gl_dir d[] = {
90 { "a", a, __arraycount(a), 0 },
91 { "a/b", b, __arraycount(b), 0 },
95 static 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",
100 static const char *glob_star_not[] = {
101 "a/1", "a/3", "a/4", "a/b",
105 trim(char *buf, size_t len, const char *name)
107 char *path = buf, *epath = buf + len;
108 while (path < epath && (*path++ = *name++) != '\0')
111 while (path > buf && *--path == '/')
116 gl_opendir(const char *dir)
119 char buf[MAXPATHLEN];
120 trim(buf, sizeof(buf), dir);
122 for (i = 0; i < __arraycount(d); i++)
123 if (strcmp(buf, d[i].name) == 0) {
124 DPRINTF(("opendir %s %zu\n", buf, i));
131 static struct dirent *
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);
141 dir.d_type = f->dir ? DT_DIR : DT_REG;
142 DPRINTF(("readdir %s %d\n", dir.d_name, dir.d_type));
144 dir.d_reclen = -1; /* Does not have _DIRENT_RECLEN */
146 dir.d_reclen = _DIRENT_RECLEN(&dir, dir.d_namlen);
154 gl_stat(const char *name , __gl_stat_t *st)
156 char buf[MAXPATHLEN];
157 trim(buf, sizeof(buf), name);
158 memset(st, 0, sizeof(*st));
160 if (strcmp(buf, "a") == 0 || strcmp(buf, "a/b") == 0) {
161 st->st_mode |= _S_IFDIR;
165 if (buf[0] == 'a' && buf[1] == '/') {
169 if (buf[2] == 'b' && buf[3] == '/') {
171 count = __arraycount(b);
175 count = __arraycount(a);
179 for (size_t i = 0; i < count; i++)
180 if (strcmp(f[i].name, buf + offs) == 0)
183 DPRINTF(("stat %s %d\n", buf, st->st_mode));
189 gl_lstat(const char *name , __gl_stat_t *st)
191 return gl_stat(name, st);
197 struct gl_dir *dd = v;
199 DPRINTF(("closedir %p\n", dd));
203 run(const char *p, int flags, const char **res, size_t len)
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;
215 RZ(glob(p, GLOB_ALTDIRFUNC | flags, NULL, &gl));
217 for (i = 0; i < gl.gl_pathc; i++)
218 DPRINTF(("%s\n", gl.gl_pathv[i]));
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]);
230 ATF_TC_HEAD(glob_star, tc)
232 atf_tc_set_md_var(tc, "descr",
233 "Test glob(3) ** with GLOB_STAR");
236 ATF_TC_BODY(glob_star, tc)
238 run("a/**", GLOB_STAR, glob_star, __arraycount(glob_star));
242 ATF_TC(glob_star_not);
243 ATF_TC_HEAD(glob_star_not, tc)
245 atf_tc_set_md_var(tc, "descr",
246 "Test glob(3) ** without GLOB_STAR");
250 ATF_TC_BODY(glob_star_not, tc)
252 run("a/**", 0, glob_star_not, __arraycount(glob_star_not));
256 ATF_TC(glob_nocheck);
257 ATF_TC_HEAD(glob_nocheck, tc)
259 atf_tc_set_md_var(tc, "descr",
260 "Test glob(3) pattern with backslash and GLOB_NOCHECK");
264 ATF_TC_BODY(glob_nocheck, tc)
266 static const char pattern[] = { 'f', 'o', 'o', '\\', ';', 'b', 'a',
268 static const char *glob_nocheck[] = {
271 run(pattern, GLOB_NOCHECK, glob_nocheck, __arraycount(glob_nocheck));
278 ATF_TP_ADD_TC(tp, glob_star);
280 ATF_TP_ADD_TC(tp, glob_star_not);
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
285 * ATF_TP_ADD_TC(tp, glob_nocheck);
288 return atf_no_error();