Luna/apps/ls.cpp
apio 48df90e636
All checks were successful
continuous-integration/drone/push Build is passing
libos+apps: Use os::*print* instead of (f)printf
2023-05-01 19:32:00 +02:00

33 lines
1.1 KiB
C++

#include <os/ArgumentParser.h>
#include <os/Directory.h>
#include <os/File.h>
Result<int> luna_main(int argc, char** argv)
{
StringView pathname;
bool show_all { false };
bool show_almost_all { false };
os::ArgumentParser parser;
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
parser.add_system_program_info("ls"_sv);
parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
parser.add_switch_argument(show_all, 'a', "all"_sv, "also list hidden files (whose filename begins with a dot)"_sv);
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
parser.parse(argc, argv);
auto dir = TRY(os::Directory::open(pathname));
auto filter = os::Directory::Filter::Hidden;
if (show_almost_all) filter = os::Directory::Filter::ParentAndBase;
else if (show_all)
filter = os::Directory::Filter::None;
auto files = TRY(dir->list(filter));
auto list = TRY(String::join(files, " "_sv));
os::println("%s", list.chars());
return 0;
}