# HG changeset patch # User Rik # Date 1599178501 25200 # Node ID 549c10384cc2c04ba650703838b00a3e78c053ed # Parent b245bcfa173629ea389658acb71e53794a1e677d dir.m: Return directory, not contents, when wildcard specified (bug #58976) * dir.m: Add logic to detect wildcard (Matlab only recognizes '*') and disable special case logic to return directory contents when only one argument to dir is present. Add BIST test for bug #58976 (this was harder than the fix). diff -r b245bcfa1736 -r 549c10384cc2 scripts/miscellaneous/dir.m --- a/scripts/miscellaneous/dir.m Thu Sep 03 15:11:49 2020 -0700 +++ b/scripts/miscellaneous/dir.m Thu Sep 03 17:15:01 2020 -0700 @@ -97,9 +97,11 @@ if (strcmp (directory, ".")) flst = {"."}; nf = 1; + dir_has_wildcard = false; else flst = __wglob__ (directory); nf = numel (flst); + dir_has_wildcard = any (directory == '*'); # See Bug #58976. endif ## Determine the file list for the case where a single directory is specified. @@ -109,7 +111,7 @@ if (err < 0) warning ("dir: 'stat (%s)' failed: %s", fn, msg); nf = 0; - elseif (S_ISDIR (st.mode)) + elseif (S_ISDIR (st.mode) && ! dir_has_wildcard) flst = readdir (flst{1}); nf = numel (flst); flst = strcat ([fn filesep], flst); @@ -228,6 +230,40 @@ %! endif %! end_unwind_protect +%!test <*58976> +%! orig_dir = pwd (); +%! tmp_dir = tempname (); +%! unwind_protect +%! assert (mkdir (tmp_dir)); +%! assert (mkdir (fullfile (tmp_dir, "dir1"))); +%! assert (mkdir (fullfile (tmp_dir, "dir2"))); +%! chdir (fullfile (tmp_dir, "dir1")); +%! fclose (fopen ("f1", "w")); +%! chdir (tmp_dir); +%! +%! ## Wildcard with multiple matches lists directories +%! list = dir (fullfile (tmp_dir, "dir*")); +%! keyboard; +%! assert (numel (list) == 2); +%! assert ({list.name}, {"dir1", "dir2"}); +%! +%! ## Wildcard with single match lists directories +%! assert (rmdir (fullfile (tmp_dir, "dir2"))); +%! list = dir (fullfile (tmp_dir, "dir*")); +%! assert (numel (list) == 1); +%! assert ({list.name}, {"dir1"}); +%! +%! ## No wildcard returns listing of directory contents +%! list = dir (fullfile (tmp_dir, "dir1")); +%! assert (any (strcmp ({list.name}, "f1"))); +%! unwind_protect_cleanup +%! chdir (orig_dir); +%! confirm_recursive_rmdir (false, "local"); +%! if (exist (tmp_dir)) +%! sts = rmdir (tmp_dir, "s"); +%! endif +%! end_unwind_protect + %!testif ; isunix () <*57666> %! orig_dir = pwd (); %! tmp_dir = tempname ();