2023-03-29 16:27:02 +00:00
|
|
|
#include <os/ArgumentParser.h>
|
2023-04-28 19:15:41 +00:00
|
|
|
#include <os/Directory.h>
|
2023-03-29 16:27:02 +00:00
|
|
|
|
2023-03-28 23:07:58 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-04-13 15:04:59 +00:00
|
|
|
Result<int> luna_main(int argc, char** argv)
|
2023-03-28 23:07:58 +00:00
|
|
|
{
|
2023-03-29 16:27:02 +00:00
|
|
|
StringView pathname;
|
2023-03-29 20:19:53 +00:00
|
|
|
bool show_all { false };
|
|
|
|
bool show_almost_all { false };
|
2023-03-29 16:27:02 +00:00
|
|
|
|
2023-04-07 08:40:46 +00:00
|
|
|
os::ArgumentParser parser;
|
2023-04-19 17:16:45 +00:00
|
|
|
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
|
2023-04-28 14:33:05 +00:00
|
|
|
parser.add_system_program_info("ls"_sv);
|
2023-04-11 20:13:54 +00:00
|
|
|
parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
|
2023-04-19 17:16:45 +00:00
|
|
|
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);
|
2023-03-29 16:27:02 +00:00
|
|
|
parser.parse(argc, argv);
|
2023-03-28 23:07:58 +00:00
|
|
|
|
2023-04-28 19:15:41 +00:00
|
|
|
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;
|
2023-03-28 23:07:58 +00:00
|
|
|
|
|
|
|
int first_ent = 1;
|
|
|
|
do {
|
2023-04-28 19:15:41 +00:00
|
|
|
auto ent = TRY(dir->next(filter));
|
|
|
|
if (ent.is_empty()) break;
|
|
|
|
printf(first_ent ? "%s" : " %s", ent.chars());
|
2023-03-28 23:07:58 +00:00
|
|
|
first_ent = 0;
|
|
|
|
} while (1);
|
|
|
|
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|