diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 47133f7d..6fc6ebda 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -23,3 +23,4 @@ luna_app(chown.cpp chown OFF) luna_app(chmod.cpp chmod OFF) luna_app(mkdir.cpp mkdir OFF) luna_app(rm.cpp rm OFF) +luna_app(stat.cpp stat OFF) diff --git a/apps/stat.cpp b/apps/stat.cpp new file mode 100644 index 00000000..49dd851f --- /dev/null +++ b/apps/stat.cpp @@ -0,0 +1,38 @@ +#include +#include +#include + +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 luna_main(int argc, char** argv) +{ + StringView path; + + os::ArgumentParser parser; + parser.add_description("Display file status."); + 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; +}