Luna/apps/stat.cpp
apio c6d91c89cd
All checks were successful
continuous-integration/drone/push Build is passing
stat: Change program description
2023-07-24 19:17:36 +02:00

46 lines
1.3 KiB
C++

#include <os/ArgumentParser.h>
#include <os/FileSystem.h>
#include <os/Mode.h>
#include <stdio.h>
#include <sys/stat.h>
static const char* file_type(mode_t mode)
{
switch (mode & S_IFMT)
{
case S_IFREG: return "regular file";
case S_IFDIR: return "directory";
case S_IFCHR: return "character special device";
case S_IFBLK: return "block special device";
case S_IFLNK: return "symbolic link";
case S_IFIFO: return "pipe";
default: return "unknown file type";
}
}
Result<int> luna_main(int argc, char** argv)
{
StringView path;
bool follow_symlinks { false };
os::ArgumentParser parser;
parser.add_description("Display file metadata.");
parser.add_system_program_info("stat"_sv);
parser.add_positional_argument(path, "path", true);
parser.add_switch_argument(follow_symlinks, 'L', "dereference"_sv, "follow symlinks");
parser.parse(argc, argv);
struct stat st;
TRY(os::FileSystem::stat(path, st, follow_symlinks));
char buf[11];
os::format_mode(st.st_mode, buf);
printf(" File: %s\n", path.chars());
printf(" Size: %zu (%s)\n", st.st_size, file_type(st.st_mode));
printf("Inode: %lu Links: %lu\n", st.st_ino, st.st_nlink);
printf(" Mode: (%#o/%s) UID: %u GID: %u\n", st.st_mode & ~S_IFMT, buf, st.st_uid, st.st_gid);
return 0;
}