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