From 7dc0511ea4be210f82abb1c82a31aec3a4fe5736 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 12 Jan 2021 18:17:02 +0900 Subject: Remove "." and ".." from Dir.glob with FNM_DOTMATCH [Bug #17280] Co-authored-by: Jeremy Evans --- dir.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'dir.c') diff --git a/dir.c b/dir.c index bedbdd9f83..48c9eaefa0 100644 --- a/dir.c +++ b/dir.c @@ -222,6 +222,7 @@ typedef enum { #define FNM_SHORTNAME 0 #endif #define FNM_GLOB_NOSORT 0x40 +#define FNM_GLOB_SKIPDOT 0x80 #define FNM_NOMATCH 1 #define FNM_ERROR 2 @@ -2413,6 +2414,10 @@ glob_helper( } return status; } + + int skipdot = (flags & FNM_GLOB_SKIPDOT); + flags |= FNM_GLOB_SKIPDOT; + while ((dp = glob_getent(&globent, flags, enc)) != NULL) { char *buf; rb_pathtype_t new_pathtype = path_unknown; @@ -2423,11 +2428,12 @@ glob_helper( name = dp->d_name; namlen = dp->d_namlen; - if (recursive && name[0] == '.') { + if (name[0] == '.') { ++dotfile; if (namlen == 1) { /* unless DOTMATCH, skip current directories not to recurse infinitely */ - if (!(flags & FNM_DOTMATCH)) continue; + if (recursive && !(flags & FNM_DOTMATCH)) continue; + if (skipdot) continue; ++dotfile; new_pathtype = path_directory; /* force to skip stat/lstat */ } -- cgit v1.2.3