2023-04-21 16:04:17 +00:00
|
|
|
#include <os/ArgumentParser.h>
|
2023-05-12 21:47:20 +00:00
|
|
|
#include <os/FileSystem.h>
|
2023-05-26 18:27:47 +00:00
|
|
|
#include <os/Mode.h>
|
2023-04-21 16:04:17 +00:00
|
|
|
#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";
|
2023-05-26 18:27:47 +00:00
|
|
|
case S_IFBLK: return "block special device";
|
2023-05-20 19:48:46 +00:00
|
|
|
case S_IFLNK: return "symbolic link";
|
2023-05-23 18:54:25 +00:00
|
|
|
case S_IFIFO: return "pipe";
|
2023-04-21 16:04:17 +00:00
|
|
|
default: return "unknown file type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<int> luna_main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
StringView path;
|
2023-05-20 19:48:46 +00:00
|
|
|
bool follow_symlinks { false };
|
2023-04-21 16:04:17 +00:00
|
|
|
|
|
|
|
os::ArgumentParser parser;
|
|
|
|
parser.add_description("Display file status.");
|
2023-04-28 14:33:05 +00:00
|
|
|
parser.add_system_program_info("stat"_sv);
|
2023-04-21 16:04:17 +00:00
|
|
|
parser.add_positional_argument(path, "path", true);
|
2023-05-20 19:48:46 +00:00
|
|
|
parser.add_switch_argument(follow_symlinks, 'L', "dereference"_sv, "follow symlinks");
|
2023-04-21 16:04:17 +00:00
|
|
|
parser.parse(argc, argv);
|
|
|
|
|
|
|
|
struct stat st;
|
2023-05-20 19:48:46 +00:00
|
|
|
TRY(os::FileSystem::stat(path, st, follow_symlinks));
|
2023-04-21 16:04:17 +00:00
|
|
|
|
2023-05-26 18:27:47 +00:00
|
|
|
char buf[11];
|
|
|
|
os::format_mode(st.st_mode, buf);
|
|
|
|
|
2023-04-21 16:04:17 +00:00
|
|
|
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);
|
2023-05-26 18:27:47 +00:00
|
|
|
printf(" Mode: (%#o/%s) UID: %u GID: %u\n", st.st_mode & ~S_IFMT, buf, st.st_uid, st.st_gid);
|
2023-04-21 16:04:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|