etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / gen / t_glob.c
blobff62b638e12c545a37ed2ace7787067e03cf22a7
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.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Christos Zoulas
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
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.
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: t_glob.c,v 1.3 2013/01/02 11:28:48 martin Exp $");
37 #include <atf-c.h>
39 #include <sys/param.h>
40 #include <sys/stat.h>
42 #include <dirent.h>
43 #include <glob.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <errno.h>
49 #include "../../../h_macros.h"
52 #ifdef DEBUG
53 #define DPRINTF(a) printf a
54 #else
55 #define DPRINTF(a)
56 #endif
58 struct gl_file {
59 const char *name;
60 int dir;
63 static struct gl_file a[] = {
64 { "1", 0 },
65 { "b", 1 },
66 { "3", 0 },
67 { "4", 0 },
70 static struct gl_file b[] = {
71 { "x", 0 },
72 { "y", 0 },
73 { "z", 0 },
74 { "w", 0 },
77 struct gl_dir {
78 const char *name; /* directory name */
79 const struct gl_file *dir;
80 size_t len, pos;
83 static struct gl_dir d[] = {
84 { "a", a, __arraycount(a), 0 },
85 { "a/b", b, __arraycount(b), 0 },
88 static const char *glob_star[] = {
89 "a/1", "a/3", "a/4", "a/b", "a/b/w", "a/b/x", "a/b/y", "a/b/z",
92 static const char *glob_star_not[] = {
93 "a/1", "a/3", "a/4", "a/b",
96 static void
97 trim(char *buf, size_t len, const char *name)
99 char *path = buf, *epath = buf + len;
100 while (path < epath && (*path++ = *name++) != '\0')
101 continue;
102 path--;
103 while (path > buf && *--path == '/')
104 *path = '\0';
107 static void *
108 gl_opendir(const char *dir)
110 size_t i;
111 char buf[MAXPATHLEN];
112 trim(buf, sizeof(buf), dir);
114 for (i = 0; i < __arraycount(d); i++)
115 if (strcmp(buf, d[i].name) == 0) {
116 DPRINTF(("opendir %s %zu\n", buf, i));
117 return &d[i];
119 errno = ENOENT;
120 return NULL;
123 static struct dirent *
124 gl_readdir(void *v)
126 static struct dirent dir;
127 struct gl_dir *dd = v;
128 if (dd->pos < dd->len) {
129 const struct gl_file *f = &dd->dir[dd->pos++];
130 strcpy(dir.d_name, f->name);
131 dir.d_namlen = strlen(f->name);
132 dir.d_ino = dd->pos;
133 dir.d_type = f->dir ? DT_DIR : DT_REG;
134 DPRINTF(("readdir %s %d\n", dir.d_name, dir.d_type));
135 dir.d_reclen = _DIRENT_RECLEN(&dir, dir.d_namlen);
136 return &dir;
138 return NULL;
141 static int
142 gl_stat(const char *name , __gl_stat_t *st)
144 char buf[MAXPATHLEN];
145 trim(buf, sizeof(buf), name);
146 memset(st, 0, sizeof(*st));
148 if (strcmp(buf, "a") == 0 || strcmp(buf, "a/b") == 0) {
149 st->st_mode |= _S_IFDIR;
150 return 0;
153 if (buf[0] == 'a' && buf[1] == '/') {
154 struct gl_file *f;
155 size_t offs, count;
157 if (buf[2] == 'b' && buf[3] == '/') {
158 offs = 4;
159 count = __arraycount(b);
160 f = b;
161 } else {
162 offs = 2;
163 count = __arraycount(a);
164 f = a;
167 for (size_t i = 0; i < count; i++)
168 if (strcmp(f[i].name, buf + offs) == 0)
169 return 0;
171 DPRINTF(("stat %s %d\n", buf, st->st_mode));
172 errno = ENOENT;
173 return -1;
176 static int
177 gl_lstat(const char *name , __gl_stat_t *st)
179 return gl_stat(name, st);
182 static void
183 gl_closedir(void *v)
185 struct gl_dir *dd = v;
186 dd->pos = 0;
187 DPRINTF(("closedir %p\n", dd));
190 static void
191 run(const char *p, int flags, const char **res, size_t len)
193 glob_t gl;
194 size_t i;
196 memset(&gl, 0, sizeof(gl));
197 gl.gl_opendir = gl_opendir;
198 gl.gl_readdir = gl_readdir;
199 gl.gl_closedir = gl_closedir;
200 gl.gl_stat = gl_stat;
201 gl.gl_lstat = gl_lstat;
203 RZ(glob(p, GLOB_ALTDIRFUNC | flags, NULL, &gl));
205 for (i = 0; i < gl.gl_pathc; i++)
206 DPRINTF(("%s\n", gl.gl_pathv[i]));
208 ATF_CHECK(len == gl.gl_pathc);
209 for (i = 0; i < gl.gl_pathc; i++)
210 ATF_CHECK_STREQ(gl.gl_pathv[i], res[i]);
212 globfree(&gl);
216 ATF_TC(glob_star);
217 ATF_TC_HEAD(glob_star, tc)
219 atf_tc_set_md_var(tc, "descr",
220 "Test glob(3) ** with GLOB_STAR");
223 ATF_TC_BODY(glob_star, tc)
225 run("a/**", GLOB_STAR, glob_star, __arraycount(glob_star));
228 ATF_TC(glob_star_not);
229 ATF_TC_HEAD(glob_star_not, tc)
231 atf_tc_set_md_var(tc, "descr",
232 "Test glob(3) ** without GLOB_STAR");
236 ATF_TC_BODY(glob_star_not, tc)
238 run("a/**", 0, glob_star_not, __arraycount(glob_star_not));
241 #if 0
242 ATF_TC(glob_nocheck);
243 ATF_TC_HEAD(glob_nocheck, tc)
245 atf_tc_set_md_var(tc, "descr",
246 "Test glob(3) pattern with backslash and GLOB_NOCHECK");
250 ATF_TC_BODY(glob_nocheck, tc)
252 static const char pattern[] = { 'f', 'o', 'o', '\\', ';', 'b', 'a',
253 'r', '\0' };
254 static const char *glob_nocheck[] = {
255 pattern
257 run(pattern, GLOB_NOCHECK, glob_nocheck, __arraycount(glob_nocheck));
259 #endif
261 ATF_TP_ADD_TCS(tp)
263 ATF_TP_ADD_TC(tp, glob_star);
264 ATF_TP_ADD_TC(tp, glob_star_not);
266 * Remove this test for now - the GLOB_NOCHECK return value has been
267 * re-defined to return a modified pattern in revision 1.33 of glob.c
269 * ATF_TP_ADD_TC(tp, glob_nocheck);
272 return atf_no_error();