From b61307e5cb645c6f6c063250169c889b6ebffda6 Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 23 May 2023 14:53:38 +0200 Subject: [PATCH] ls: Add -1 and --directory --- apps/ls.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/apps/ls.cpp b/apps/ls.cpp index b0bca358..4a100033 100644 --- a/apps/ls.cpp +++ b/apps/ls.cpp @@ -28,6 +28,8 @@ Result luna_main(int argc, char** argv) bool human_readable { false }; bool si { false }; bool follow_symlink_args { false }; + bool one_per_line { false }; + bool list_directories { false }; os::ArgumentParser parser; parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv); @@ -41,13 +43,17 @@ Result luna_main(int argc, char** argv) parser.add_switch_argument(si, ' ', "si"_sv, "same as -h, but show sizes in powers of 10"_sv); parser.add_switch_argument(follow_symlink_args, 'H', "dereference-args"_sv, "follow symbolic links listed as arguments"_sv); + parser.add_switch_argument(one_per_line, '1', ""_sv, "list one file per line"_sv); + parser.add_switch_argument(list_directories, 'd', "directory"_sv, "list directories instead of their contents"_sv); parser.parse(argc, argv); Vector files; int dirfd = AT_FDCWD; SharedPtr dir; - if (os::FileSystem::is_directory(pathname, follow_symlink_args)) + if (!long_list) follow_symlink_args = true; + + if (os::FileSystem::is_directory(pathname, follow_symlink_args) && !list_directories) { dir = TRY(os::Directory::open(pathname)); dirfd = dir->fd(); @@ -69,7 +75,7 @@ Result luna_main(int argc, char** argv) if (!long_list) { - auto list = TRY(String::join(files, " "_sv)); + auto list = TRY(String::join(files, one_per_line ? "\n"_sv : " "_sv)); if (!list.is_empty()) os::println("%s", list.chars()); } else