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