1 /* $NetBSD: t_glob.c,v 1.6 2017/04/26 14:52:57 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.6 2017/04/26 14:52:57 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 static const char *glob_range[] = { 89 "a/b/x", "a/b/y", "a/b/z", 90 }; 91 92 static const char *glob_range_not[] = { 93 "a/b/w", 94 }; 95 96 static const char *glob_star[] = { 97 "a/1", "a/3", "a/4", "a/b", "a/b/w", "a/b/x", "a/b/y", "a/b/z", 98 }; 99 100 static const char *glob_star_not[] = { 101 "a/1", "a/3", "a/4", "a/b", 102 }; 103 104 static void 105 trim(char *buf, size_t len, const char *name) 106 { 107 char *path = buf, *epath = buf + len; 108 while (path < epath && (*path++ = *name++) != '\0') 109 continue; 110 path--; 111 while (path > buf && *--path == '/') 112 *path = '\0'; 113 } 114 115 static void * 116 gl_opendir(const char *dir) 117 { 118 size_t i; 119 char buf[MAXPATHLEN]; 120 trim(buf, sizeof(buf), dir); 121 122 for (i = 0; i < __arraycount(d); i++) 123 if (strcmp(buf, d[i].name) == 0) { 124 DPRINTF(("opendir %s %zu\n", buf, i)); 125 return &d[i]; 126 } 127 errno = ENOENT; 128 return NULL; 129 } 130 131 static struct dirent * 132 gl_readdir(void *v) 133 { 134 static struct dirent dir; 135 struct gl_dir *dd = v; 136 if (dd->pos < dd->len) { 137 const struct gl_file *f = &dd->dir[dd->pos++]; 138 strcpy(dir.d_name, f->name); 139 dir.d_namlen = strlen(f->name); 140 dir.d_ino = dd->pos; 141 dir.d_type = f->dir ? DT_DIR : DT_REG; 142 DPRINTF(("readdir %s %d\n", dir.d_name, dir.d_type)); 143 dir.d_reclen = _DIRENT_RECLEN(&dir, dir.d_namlen); 144 return &dir; 145 } 146 return NULL; 147 } 148 149 static int 150 gl_stat(const char *name , __gl_stat_t *st) 151 { 152 char buf[MAXPATHLEN]; 153 trim(buf, sizeof(buf), name); 154 memset(st, 0, sizeof(*st)); 155 156 if (strcmp(buf, "a") == 0 || strcmp(buf, "a/b") == 0) { 157 st->st_mode |= S_IFDIR; 158 return 0; 159 } 160 161 if (buf[0] == 'a' && buf[1] == '/') { 162 struct gl_file *f; 163 size_t offs, count; 164 165 if (buf[2] == 'b' && buf[3] == '/') { 166 offs = 4; 167 count = __arraycount(b); 168 f = b; 169 } else { 170 offs = 2; 171 count = __arraycount(a); 172 f = a; 173 } 174 175 for (size_t i = 0; i < count; i++) 176 if (strcmp(f[i].name, buf + offs) == 0) 177 return 0; 178 } 179 DPRINTF(("stat %s %d\n", buf, st->st_mode)); 180 errno = ENOENT; 181 return -1; 182 } 183 184 static int 185 gl_lstat(const char *name , __gl_stat_t *st) 186 { 187 return gl_stat(name, st); 188 } 189 190 static void 191 gl_closedir(void *v) 192 { 193 struct gl_dir *dd = v; 194 dd->pos = 0; 195 DPRINTF(("closedir %p\n", dd)); 196 } 197 198 static void 199 run(const char *p, int flags, const char **res, size_t len) 200 { 201 glob_t gl; 202 size_t i; 203 int e; 204 205 DPRINTF(("pattern %s\n", p)); 206 memset(&gl, 0, sizeof(gl)); 207 gl.gl_opendir = gl_opendir; 208 gl.gl_readdir = gl_readdir; 209 gl.gl_closedir = gl_closedir; 210 gl.gl_stat = gl_stat; 211 gl.gl_lstat = gl_lstat; 212 213 switch ((e = glob(p, GLOB_ALTDIRFUNC | flags, NULL, &gl))) { 214 case 0: 215 break; 216 case GLOB_NOSPACE: 217 fprintf(stderr, "Malloc call failed.\n"); 218 goto bad; 219 case GLOB_ABORTED: 220 fprintf(stderr, "Unignored error.\n"); 221 goto bad; 222 case GLOB_NOMATCH: 223 fprintf(stderr, "No match, and GLOB_NOCHECK was not set.\n"); 224 goto bad; 225 case GLOB_NOSYS: 226 fprintf(stderr, "Implementation does not support function.\n"); 227 goto bad; 228 default: 229 fprintf(stderr, "Unknown error %d.\n", e); 230 goto bad; 231 } 232 233 for (i = 0; i < gl.gl_pathc; i++) 234 DPRINTF(("%s\n", gl.gl_pathv[i])); 235 236 ATF_CHECK(len == gl.gl_pathc); 237 for (i = 0; i < gl.gl_pathc && i < len; i++) 238 ATF_CHECK_STREQ(gl.gl_pathv[i], res[i]); 239 240 globfree(&gl); 241 return; 242 bad: 243 ATF_REQUIRE_MSG(e == 0, "No match for `%s'", p); 244 } 245 246 247 ATF_TC(glob_range); 248 ATF_TC_HEAD(glob_range, tc) 249 { 250 atf_tc_set_md_var(tc, "descr", 251 "Test glob(3) range"); 252 } 253 254 ATF_TC_BODY(glob_range, tc) 255 { 256 run("a/b/[x-z]", 0, glob_range, __arraycount(glob_range)); 257 } 258 259 ATF_TC(glob_range_not); 260 ATF_TC_HEAD(glob_range_not, tc) 261 { 262 atf_tc_set_md_var(tc, "descr", 263 "Test glob(3) ! range"); 264 } 265 266 ATF_TC_BODY(glob_range_not, tc) 267 { 268 run("a/b/[!x-z]", 0, glob_range_not, __arraycount(glob_range_not)); 269 } 270 271 ATF_TC(glob_star); 272 ATF_TC_HEAD(glob_star, tc) 273 { 274 atf_tc_set_md_var(tc, "descr", 275 "Test glob(3) ** with GLOB_STAR"); 276 } 277 278 ATF_TC_BODY(glob_star, tc) 279 { 280 run("a/**", GLOB_STAR, glob_star, __arraycount(glob_star)); 281 } 282 283 ATF_TC(glob_star_not); 284 ATF_TC_HEAD(glob_star_not, tc) 285 { 286 atf_tc_set_md_var(tc, "descr", 287 "Test glob(3) ** without GLOB_STAR"); 288 } 289 290 291 ATF_TC_BODY(glob_star_not, tc) 292 { 293 run("a/**", 0, glob_star_not, __arraycount(glob_star_not)); 294 } 295 296 #if 0 297 ATF_TC(glob_nocheck); 298 ATF_TC_HEAD(glob_nocheck, tc) 299 { 300 atf_tc_set_md_var(tc, "descr", 301 "Test glob(3) pattern with backslash and GLOB_NOCHECK"); 302 } 303 304 305 ATF_TC_BODY(glob_nocheck, tc) 306 { 307 static const char pattern[] = { 'f', 'o', 'o', '\\', ';', 'b', 'a', 308 'r', '\0' }; 309 static const char *glob_nocheck[] = { 310 pattern 311 }; 312 run(pattern, GLOB_NOCHECK, glob_nocheck, __arraycount(glob_nocheck)); 313 } 314 #endif 315 316 ATF_TP_ADD_TCS(tp) 317 { 318 ATF_TP_ADD_TC(tp, glob_star); 319 ATF_TP_ADD_TC(tp, glob_star_not); 320 ATF_TP_ADD_TC(tp, glob_range); 321 ATF_TP_ADD_TC(tp, glob_range_not); 322 /* 323 * Remove this test for now - the GLOB_NOCHECK return value has been 324 * re-defined to return a modified pattern in revision 1.33 of glob.c 325 * 326 * ATF_TP_ADD_TC(tp, glob_nocheck); 327 */ 328 329 return atf_no_error(); 330 } 331