From 771fa2b51c6a7e0368c8ce68ff7a0c7a349fa863 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 20 Aug 2023 20:44:11 +0200 Subject: [PATCH] - rewrote dir CCMD using ScanDirectory and avoiding I_ChDir entirely. --- src/common/console/c_enginecmds.cpp | 82 +++++++++-------------------- 1 file changed, 24 insertions(+), 58 deletions(-) diff --git a/src/common/console/c_enginecmds.cpp b/src/common/console/c_enginecmds.cpp index 92b8cea53..6c914db66 100644 --- a/src/common/console/c_enginecmds.cpp +++ b/src/common/console/c_enginecmds.cpp @@ -176,81 +176,47 @@ UNSAFE_CCMD (crashout) #endif -// commented out because it's very poorly implemented (it should not abuse I_ChDir like this!) -#if 0 UNSAFE_CCMD (dir) { - FString dir, path; - const char *match; - findstate_t c_file; - void *file; - - FString curdir = I_GetCWD(); - if (curdir.IsEmpty()) - { - Printf ("Current path too long\n"); - return; - } + FString path; if (argv.argc() > 1) { path = NicePath(argv[1]); - if (!I_ChDir(path)) - { - match = path; - dir = ExtractFilePath(path); - if (dir[0] != '\0') - { - match += dir.Len(); - } - else - { - dir = "./"; - } - if (match[0] == '\0') - { - match = "*"; - } - if (!I_ChDir(dir)) - { - Printf ("%s not found\n", dir.GetChars()); - return; - } - } - else - { - match = "*"; - dir = path; - } } else { - match = "*"; - dir = curdir; + path = I_GetCWD();; } - if (dir[dir.Len()-1] != '/') + auto base = ExtractFileBase(path, true); + FString bpath; + if (base.IndexOfAny("*?") >= 0) { - dir += '/'; + bpath = ExtractFilePath(path); } - - if ( (file = I_FindFirst (match, &c_file)) == ((void *)(-1))) - Printf ("Nothing matching %s%s\n", dir.GetChars(), match); else { - Printf ("Listing of %s%s:\n", dir.GetChars(), match); - do - { - if (I_FindAttr (&c_file) & FA_DIREC) - Printf (PRINT_BOLD, "%s \n", I_FindName (&c_file)); - else - Printf ("%s\n", I_FindName (&c_file)); - } while (I_FindNext (file, &c_file) == 0); - I_FindClose (file); + base = "*"; + bpath = path; } - I_ChDir(curdir); + FileList list; + if (!ScanDirectory(list, bpath, base, true)) + { + Printf ("Nothing matching %s\n", path.GetChars()); + } + else + { + Printf ("Listing of %s:\n", path.GetChars()); + for(auto& entry : list) + { + if (entry.isDirectory) + Printf (PRINT_BOLD, "%s \n", entry.FileName.c_str()); + else + Printf ("%s\n", entry.FileName.c_str()); + } + } } -#endif //========================================================================== //