Luna/apps/stat.cpp

40 lines
1007 B
C++
Raw Normal View History

2023-04-21 16:04:17 +00:00
#include <os/ArgumentParser.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";
default: return "unknown file type";
}
}
Result<int> luna_main(int argc, char** argv)
{
StringView path;
os::ArgumentParser parser;
parser.add_description("Display file status.");
parser.add_system_program_info("stat"_sv);
2023-04-21 16:04:17 +00:00
parser.add_positional_argument(path, "path", true);
parser.parse(argc, argv);
struct stat st;
if (stat(path.chars(), &st) < 0)
{
perror("stat");
return 1;
}
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 UID: %u GID: %u\n", st.st_mode & ~S_IFMT, st.st_uid, st.st_gid);
return 0;
}