Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/lib/libc/gen/glob.c,v retrieving revision 1.18 retrieving revision 1.18.8.2 diff -u -p -r1.18 -r1.18.8.2 --- src/lib/libc/gen/glob.c 2006/12/01 18:57:29 1.18 +++ src/lib/libc/gen/glob.c 2008/03/23 00:40:25 1.18.8.2 @@ -1,4 +1,4 @@ -/* $NetBSD: glob.c,v 1.18 2006/12/01 18:57:29 christos Exp $ */ +/* glob.c,v 1.18.8.1 2008/01/09 01:34:05 matt Exp */ /* * Copyright (c) 1989, 1993 @@ -37,7 +37,7 @@ #if 0 static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; #else -__RCSID("$NetBSD: glob.c,v 1.18 2006/12/01 18:57:29 christos Exp $"); +__RCSID("glob.c,v 1.18.8.1 2008/01/09 01:34:05 matt Exp"); #endif #endif /* LIBC_SCCS and not lint */ @@ -59,6 +59,10 @@ __RCSID("$NetBSD: glob.c,v 1.18 2006/12/ * expand ~user/foo to the /home/dir/of/user/foo * GLOB_BRACE: * expand {1,2}{a,b} to 1a 1b 2a 2b + * GLOB_PERIOD: + * allow metacharacters to match leading dots in filenames. + * GLOB_NO_DOTDIRS: + * . and .. are hidden from wildcards, even if GLOB_PERIOD is set. * gl_matchc: * Number of matches in the current invocation of glob. */ @@ -74,6 +78,7 @@ __RCSID("$NetBSD: glob.c,v 1.18 2006/12/ #include #include #include +#include #include #include #include @@ -582,6 +587,8 @@ glob2(Char *pathbuf, Char *pathend, Char __gl_stat_t sb; Char *p, *q; int anymeta; + Char *pend; + ptrdiff_t diff; _DIAGASSERT(pathbuf != NULL); _DIAGASSERT(pathend != NULL); @@ -623,7 +630,26 @@ glob2(Char *pathbuf, Char *pathend, Char *q++ = *p++; } - if (!anymeta) { /* No expansion, do next segment. */ + /* + * No expansion, or path ends in slash-dot shash-dot-dot, + * do next segment. + */ + if (pglob->gl_flags & GLOB_PERIOD) { + for (pend = pathend; pend > pathbuf && pend[-1] == '/'; + pend--) + continue; + diff = pend - pathbuf; + } else { + /* XXX: GCC */ + diff = 0; + pend = pathend; + } + + if ((!anymeta) || + ((pglob->gl_flags & GLOB_PERIOD) && + (diff >= 1 && pend[-1] == DOT) && + (diff >= 2 && (pend[-2] == SLASH || pend[-2] == DOT)) && + (diff < 3 || pend[-3] == SLASH))) { pathend = q; pattern = p; while (*pattern == SEP) { @@ -695,8 +721,20 @@ glob3(Char *pathbuf, Char *pathend, Char u_char *sc; Char *dc; - /* Initial DOT must be matched literally. */ - if (dp->d_name[0] == DOT && *pattern != DOT) + /* + * Initial DOT must be matched literally, unless we have + * GLOB_PERIOD set. + */ + if ((pglob->gl_flags & GLOB_PERIOD) == 0) + if (dp->d_name[0] == DOT && *pattern != DOT) + continue; + /* + * If GLOB_NO_DOTDIRS is set, . and .. vanish. + */ + if ((pglob->gl_flags & GLOB_NO_DOTDIRS) && + (dp->d_name[0] == DOT) && + ((dp->d_name[1] == EOS) || + ((dp->d_name[1] == DOT) && (dp->d_name[2] == EOS)))) continue; /* * The resulting string contains EOS, so we can