Luna/apps/ls.cpp
apio e6645ed607
StringView+apps: Add a _sv literal suffix to not confuse function overloads
C++ was being naughty and implicitly casting our fallback const char* to a boolean.
2023-03-29 22:10:01 +02:00

42 lines
1009 B
C++

#include <os/ArgumentParser.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv)
{
StringView pathname;
bool show_all;
bool show_almost_all;
ArgumentParser parser;
parser.add_positional_argument(pathname, "directory"_sv, "/"_sv);
parser.add_switch_argument(show_all, 'a', "all"_sv);
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);
parser.parse(argc, argv);
DIR* dp = opendir(pathname.chars());
if (!dp)
{
perror("opendir");
return 1;
}
int first_ent = 1;
do {
struct dirent* ent = readdir(dp);
if (!ent) break;
if (show_almost_all && (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))) continue;
if (!show_all && !show_almost_all && *ent->d_name == '.') continue;
printf(first_ent ? "%s" : " %s", ent->d_name);
first_ent = 0;
} while (1);
putchar('\n');
closedir(dp);
return 0;
}