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