From 51f49ec73f74f935c3ee88561cf73323fd8d841b Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Tue, 10 Nov 2020 04:13:46 +0000 Subject: [PATCH] ls: Add -A flag to show dot files excluding implied . and .. directories --- Userland/ls.cpp | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/Userland/ls.cpp b/Userland/ls.cpp index f633512deb..479dac5515 100644 --- a/Userland/ls.cpp +++ b/Userland/ls.cpp @@ -54,6 +54,7 @@ static int do_file_system_object_short(const char* path); static bool flag_colorize = false; static bool flag_long = false; static bool flag_show_dotfiles = false; +static bool flag_show_almost_all_dotfiles = false; static bool flag_show_inode = false; static bool flag_print_numeric = false; static bool flag_human_readable = false; @@ -97,6 +98,7 @@ int main(int argc, char** argv) Core::ArgsParser args_parser; args_parser.add_option(flag_show_dotfiles, "Show dotfiles", "all", 'a'); + args_parser.add_option(flag_show_almost_all_dotfiles, "Do not list implied . and .. directories", nullptr, 'A'); args_parser.add_option(flag_long, "Display long info", "long", 'l'); args_parser.add_option(flag_sort_by_timestamp, "Sort files by timestamp", nullptr, 't'); args_parser.add_option(flag_reverse_sort, "Reverse sort order", "reverse", 'r'); @@ -108,6 +110,9 @@ int main(int argc, char** argv) args_parser.add_positional_argument(paths, "Directory to list", "path", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); + if (flag_show_almost_all_dotfiles) + flag_show_dotfiles = true; + if (flag_long) { setpwent(); for (auto* pwd = getpwent(); pwd; pwd = getpwent()) @@ -332,8 +337,19 @@ static int do_file_system_object_long(const char* path) FileMetadata metadata; metadata.name = di.next_path(); ASSERT(!metadata.name.is_empty()); - if (metadata.name[0] == '.' && !flag_show_dotfiles) - continue; + + if (metadata.name[0] == '.') { + if (!flag_show_dotfiles) + continue; + if (flag_show_almost_all_dotfiles) { + // skip '.' and '..' directories + if (metadata.name.length() == 1) + continue; + if (metadata.name.length() == 2 && metadata.name[1] == '.') + continue; + } + } + StringBuilder builder; builder.append(path); builder.append('/'); @@ -403,6 +419,19 @@ int do_file_system_object_short(const char* path) size_t longest_name = 0; while (di.has_next()) { String name = di.next_path(); + + if (name[0] == '.') { + if (!flag_show_dotfiles) + continue; + if (flag_show_almost_all_dotfiles) { + // skip '.' and '..' directories + if (name.length() == 1) + continue; + if (name.length() == 2 && name[1] == '.') + continue; + } + } + names.append(name); if (names.last().length() > longest_name) longest_name = name.length();