diff --git a/dir.c b/dir.c index c02c071b97..f4608bcab4 100644 --- a/dir.c +++ b/dir.c @@ -1896,6 +1896,9 @@ glob_helper( int plain = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0; int escape = !(flags & FNM_NOESCAPE); size_t pathlen = baselen + namelen; + const char *base = path; + + if (fd != AT_FDCWD && *(base += baselen) == '/') base++; for (cur = beg; cur < end; ++cur) { struct glob_pattern *p = *cur; @@ -1928,9 +1931,9 @@ glob_helper( } } - if (*path) { + if (*base) { if (match_all && pathtype == path_unknown) { - if (do_lstat(fd, path, &st, flags, enc) == 0) { + if (do_lstat(fd, base, &st, flags, enc) == 0) { pathtype = IFTODT(st.st_mode); } else { @@ -1938,7 +1941,7 @@ glob_helper( } } if (match_dir && pathtype == path_unknown) { - if (do_stat(fd, path, &st, flags, enc) == 0) { + if (do_stat(fd, base, &st, flags, enc) == 0) { pathtype = IFTODT(st.st_mode); } else { @@ -1980,7 +1983,7 @@ glob_helper( # else ; # endif - dirp = do_opendir(fd, *path ? path : ".", flags, enc, funcs->error, arg, &status); + dirp = do_opendir(fd, *base ? base : ".", flags, enc, funcs->error, arg, &status); if (dirp == NULL) { # if FNM_SYSCASE || NORMALIZE_UTF8PATH if ((magical < 2) && !recursive && (errno == EACCES)) { @@ -1990,7 +1993,7 @@ glob_helper( # endif return status; } - IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path : ".")); + IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *base ? base : ".")); # if NORMALIZE_UTF8PATH if (!(norm_p || magical || recursive)) { diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 1c338030f3..5a2529ca97 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -207,6 +207,7 @@ class TestDir < Test::Unit::TestCase files.each {|n| File.write(File.join(@root, n), "")} assert_equal(files, Dir.glob("*/*.c", base: @root).sort) assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: ".").sort}) + assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.glob("*.c", base: "a").sort}) assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: "").sort}) assert_equal(files, Dir.chdir(@root) {Dir.glob("*/*.c", base: nil).sort}) end @@ -215,6 +216,7 @@ class TestDir < Test::Unit::TestCase files = %w[a/foo.c c/bar.c] files.each {|n| File.write(File.join(@root, n), "")} assert_equal(files, Dir.open(@root) {|d| Dir.glob("*/*.c", base: d)}.sort) + assert_equal(%w[foo.c], Dir.chdir(@root) {Dir.open("a") {|d| Dir.glob("*", base: d)}}) end def assert_entries(entries, children = false)