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