diff liboctave/util/oct-glob.cc @ 30979:302faf5bc425 stable

__wglob__: Correctly handle "." and ".." in patterns on Windows (bug #62414). * liboctave/util/oct-glob.cc (find_files): Retain "." and ".." literally in result. * libinterp/corefcn/dirfns.cc (F__wglob__): Add tests. Move test for "Fglob" immediately below function definition.
author Markus Mützel <markus.muetzel@gmx.de>
date Thu, 05 May 2022 19:23:06 +0200
parents 796f54d4ddbf
children 8475b51b990c
line wrap: on
line diff
--- a/liboctave/util/oct-glob.cc	Sun May 01 11:38:16 2022 +0200
+++ b/liboctave/util/oct-glob.cc	Thu May 05 19:23:06 2022 +0200
@@ -157,6 +157,24 @@
     find_files (std::list<std::string>& dirlist, const std::string& dir,
                 const std::string& pat, std::string& file)
     {
+      if (! pat.compare (".") || ! pat.compare (".."))
+        {
+          // shortcut for trivial patterns that would expand to a folder name
+
+          // get next component of path (or file name)
+          std::size_t sep_pos
+            = file.find_first_of (sys::file_ops::dir_sep_chars ());
+          std::string pat_str = file.substr (0, sep_pos);
+          std::string file_str = (sep_pos != std::string::npos
+                                  && file.length () > sep_pos+1)
+                                 ? file.substr (sep_pos+1) : "";
+
+          // call this function recursively with next path component in PAT
+          find_files (dirlist, sys::file_ops::concat (dir, pat),
+                      pat_str, file_str);
+          return;
+        }
+
       // remove leading file separators
       while (file.length () > 1 && sys::file_ops::is_dir_sep (file[0]))
         file = file.substr (1, std::string::npos);
@@ -178,6 +196,7 @@
 
           if (file.empty ())
             {
+              // Don't include "." and ".." in matches.
               if (found_dir.compare (".") && found_dir.compare (".."))
                 dirlist.push_back (sys::file_ops::concat (dir, found_dir));
             }