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