ls: Add the -l flag
This commit is contained in:
parent
2572d5b238
commit
53ec448e33
54
apps/ls.cpp
54
apps/ls.cpp
@ -1,12 +1,18 @@
|
|||||||
#include <os/ArgumentParser.h>
|
#include <os/ArgumentParser.h>
|
||||||
#include <os/Directory.h>
|
#include <os/Directory.h>
|
||||||
#include <os/File.h>
|
#include <os/File.h>
|
||||||
|
#include <os/FileSystem.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
Result<int> luna_main(int argc, char** argv)
|
Result<int> luna_main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
StringView pathname;
|
StringView pathname;
|
||||||
bool show_all { false };
|
bool show_all { false };
|
||||||
bool show_almost_all { false };
|
bool show_almost_all { false };
|
||||||
|
bool long_list { false };
|
||||||
|
|
||||||
os::ArgumentParser parser;
|
os::ArgumentParser parser;
|
||||||
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
|
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
|
||||||
@ -14,19 +20,51 @@ Result<int> luna_main(int argc, char** argv)
|
|||||||
parser.add_positional_argument(pathname, "directory"_sv, "."_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_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.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
|
||||||
|
parser.add_switch_argument(long_list, 'l', ""_sv, "use a long listing format"_sv);
|
||||||
parser.parse(argc, argv);
|
parser.parse(argc, argv);
|
||||||
|
|
||||||
auto dir = TRY(os::Directory::open(pathname));
|
Vector<String> files;
|
||||||
|
int dirfd = AT_FDCWD;
|
||||||
|
SharedPtr<os::Directory> dir;
|
||||||
|
|
||||||
auto filter = os::Directory::Filter::Hidden;
|
if (os::FileSystem::is_directory(pathname))
|
||||||
if (show_almost_all) filter = os::Directory::Filter::ParentAndBase;
|
{
|
||||||
else if (show_all)
|
dir = TRY(os::Directory::open(pathname));
|
||||||
filter = os::Directory::Filter::None;
|
dirfd = dir->fd();
|
||||||
|
|
||||||
auto files = TRY(dir->list(filter));
|
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 list = TRY(String::join(files, " "_sv));
|
files = TRY(dir->list(filter));
|
||||||
os::println("%s", list.chars());
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto str = TRY(String::from_cstring(pathname.chars()));
|
||||||
|
TRY(files.try_append(move(str)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!long_list)
|
||||||
|
{
|
||||||
|
auto list = TRY(String::join(files, " "_sv));
|
||||||
|
os::println("%s", list.chars());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (const auto& file : files)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
if (fstatat(dirfd, file.chars(), &st, 0) < 0)
|
||||||
|
{
|
||||||
|
perror(file.chars());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
os::println("%6o %u %4u %4u %10lu %s", st.st_mode, st.st_nlink, st.st_uid, st.st_gid, st.st_size,
|
||||||
|
file.chars());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user