]> git.saurik.com Git - apple/libc.git/blame - tests/netbsd_glob.c
Libc-1439.100.3.tar.gz
[apple/libc.git] / tests / netbsd_glob.c
CommitLineData
b061a43b
A
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 <sys/param.h>
38#include <sys/stat.h>
39
40#include <dirent.h>
41#include <glob.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <errno.h>
46
47#include <darwintest.h>
48
49#define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
50
51#ifdef DEBUG
52#define DPRINTF(a) printf a
53#else
54#define DPRINTF(a)
55#endif
56
57struct gl_file {
58 const char *name;
59 int dir;
60};
61
62static struct gl_file a[] = {
63 { "1", 0 },
64 { "b", 1 },
65 { "3", 0 },
66 { "4", 0 },
67};
68
69static struct gl_file b[] = {
70 { "x", 0 },
71 { "y", 0 },
72 { "z", 0 },
73 { "w", 0 },
74};
75
76struct gl_dir {
77 const char *name; /* directory name */
78 const struct gl_file *dir;
79 size_t len, pos;
80};
81
82static struct gl_dir d[] = {
83 { "a", a, __arraycount(a), 0 },
84 { "a/b", b, __arraycount(b), 0 },
85};
86
87#ifdef GLOB_STAR
88static 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",
90};
91#endif
92
93static const char *glob_star_not[] = {
94 "a/1", "a/3", "a/4", "a/b",
95};
96
97static void
98trim(char *buf, size_t len, const char *name)
99{
100 char *path = buf, *epath = buf + len;
101 while (path < epath && (*path++ = *name++) != '\0')
102 continue;
103 path--;
104 while (path > buf && *--path == '/')
105 *path = '\0';
106}
107
108static void *
109gl_opendir(const char *dir)
110{
111 size_t i;
112 char buf[MAXPATHLEN];
113 trim(buf, sizeof(buf), dir);
114
115 for (i = 0; i < __arraycount(d); i++)
116 if (strcmp(buf, d[i].name) == 0) {
117 DPRINTF(("opendir %s %zu\n", buf, i));
118 return &d[i];
119 }
120 errno = ENOENT;
121 return NULL;
122}
123
124static struct dirent *
125gl_readdir(void *v)
126{
127 static struct dirent dir;
128 struct gl_dir *dd = v;
129 if (dd->pos < dd->len) {
130 const struct gl_file *f = &dd->dir[dd->pos++];
131 strcpy(dir.d_name, f->name);
132 dir.d_namlen = (uint16_t)strlen(f->name);
133 dir.d_ino = dd->pos;
134 dir.d_type = f->dir ? DT_DIR : DT_REG;
135 DPRINTF(("readdir %s %d\n", dir.d_name, dir.d_type));
136#if defined(__FreeBSD__) || defined(__APPLE__)
137 dir.d_reclen = (uint16_t)-1; /* Does not have _DIRENT_RECLEN */
138#else
139 dir.d_reclen = _DIRENT_RECLEN(&dir, dir.d_namlen);
140#endif
141 return &dir;
142 }
143 return NULL;
144}
145
146static int
147gl_stat(const char *name , struct stat *st)
148{
149 char buf[MAXPATHLEN];
150 trim(buf, sizeof(buf), name);
151 memset(st, 0, sizeof(*st));
152
153 if (strcmp(buf, "a") == 0 || strcmp(buf, "a/b") == 0) {
154 st->st_mode |= S_IFDIR;
155 return 0;
156 }
157
158 if (buf[0] == 'a' && buf[1] == '/') {
159 struct gl_file *f;
160 size_t offs, count;
161
162 if (buf[2] == 'b' && buf[3] == '/') {
163 offs = 4;
164 count = __arraycount(b);
165 f = b;
166 } else {
167 offs = 2;
168 count = __arraycount(a);
169 f = a;
170 }
171
172 for (size_t i = 0; i < count; i++)
173 if (strcmp(f[i].name, buf + offs) == 0)
174 return 0;
175 }
176 DPRINTF(("stat %s %d\n", buf, st->st_mode));
177 errno = ENOENT;
178 return -1;
179}
180
181static int
182gl_lstat(const char *name , struct stat *st)
183{
184 return gl_stat(name, st);
185}
186
187static void
188gl_closedir(void *v)
189{
190 struct gl_dir *dd = v;
191 dd->pos = 0;
192 DPRINTF(("closedir %p\n", dd));
193}
194
195static void
196run(const char *p, int flags, const char **res, size_t len)
197{
198 glob_t gl;
199 size_t i;
200
201 memset(&gl, 0, sizeof(gl));
202 gl.gl_opendir = gl_opendir;
203 gl.gl_readdir = gl_readdir;
204 gl.gl_closedir = gl_closedir;
205 gl.gl_stat = gl_stat;
206 gl.gl_lstat = gl_lstat;
207
208 T_ASSERT_POSIX_ZERO(glob(p, GLOB_ALTDIRFUNC | flags, NULL, &gl), NULL);
209
210 for (i = 0; i < gl.gl_pathc; i++)
211 DPRINTF(("%s\n", gl.gl_pathv[i]));
212
213 T_EXPECT_EQ(len, gl.gl_pathc, NULL);
214 for (i = 0; i < gl.gl_pathc; i++)
215 T_EXPECT_EQ_STR(gl.gl_pathv[i], res[i], NULL);
216
217 globfree(&gl);
218}
219
220
221#ifdef GLOB_STAR
222T_DECL(glob_star, "Test glob(3) ** with GLOB_STAR")
223{
224 run("a/**", GLOB_STAR, glob_star, __arraycount(glob_star));
225}
226#endif
227
228T_DECL(glob_star_not, "Test glob(3) ** without GLOB_STAR")
229{
230 run("a/**", 0, glob_star_not, __arraycount(glob_star_not));
231}
232
233#if 0
234/*
235 * Remove this test for now - the GLOB_NOCHECK return value has been
236 * re-defined to return a modified pattern in revision 1.33 of glob.c
237 *
238 * ATF_TP_ADD_TC(tp, glob_nocheck);
239 */
240T_DECL(glob_nocheck, "Test glob(3) pattern with backslash and GLOB_NOCHECK")
241{
242 static const char pattern[] = { 'f', 'o', 'o', '\\', ';', 'b', 'a',
243 'r', '\0' };
244 static const char *glob_nocheck[] = {
245 pattern
246 };
247 run(pattern, GLOB_NOCHECK, glob_nocheck, __arraycount(glob_nocheck));
248}
249#endif