apps: Add a 'stat' utility

This commit is contained in:
apio 2022-10-28 17:14:20 +02:00
parent b1729689df
commit 2269ec267c
2 changed files with 56 additions and 1 deletions

View File

@ -1,4 +1,4 @@
APPS := init sym sh crash uname uptime hello ps ls args cat APPS := init sym sh crash uname uptime hello ps ls args cat stat
APPS_DIR := $(LUNA_ROOT)/apps APPS_DIR := $(LUNA_ROOT)/apps
APPS_SRC := $(APPS_DIR)/src APPS_SRC := $(APPS_DIR)/src

55
apps/src/stat.c Normal file
View File

@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
const char* mode_to_string(mode_t mode)
{
static char mode_string[12];
char mode_set[12] = {'s', 'g', 'r', 'w', 'x', 'r', 'w', 'x', 'r', 'w', 'x', 0};
mode_t mode_val[12] = {S_ISUID, S_ISGID, S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP,
S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH, S_IFMT};
for (int i = 0; i < 12; i++)
{
if (mode & mode_val[i]) mode_string[i] = mode_set[i];
else
mode_string[i] = '-';
}
return mode_string;
}
int main(int argc, char** argv)
{
if (argc == 1)
{
fprintf(stderr, "Usage: stat [file]\n");
return EXIT_FAILURE;
}
struct stat st;
if (stat(argv[1], &st) < 0)
{
perror("stat");
return EXIT_FAILURE;
}
printf("Type: ");
switch (st.st_mode & S_IFMT)
{
case S_IFREG: puts("Regular file"); break;
case S_IFDIR: puts("Directory"); break;
case S_IFCHR: puts("Character device"); break;
default: puts("Unknown"); break;
}
printf("Length: %ld\n", st.st_size);
printf("Inode: %ld\n", st.st_ino);
printf("UID: %d\n", st.st_uid);
printf("GID: %d\n", st.st_gid);
printf("Mode: %s\n", mode_to_string(st.st_mode));
return EXIT_SUCCESS;
}