2022-10-28 19:00:33 +00:00
|
|
|
#include <pwd.h>
|
2022-10-28 15:14:20 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2022-10-28 19:00:33 +00:00
|
|
|
struct passwd* own = getpwuid(st.st_uid);
|
|
|
|
|
2022-10-28 15:14:20 +00:00
|
|
|
printf("Length: %ld\n", st.st_size);
|
|
|
|
printf("Inode: %ld\n", st.st_ino);
|
2022-10-28 19:00:33 +00:00
|
|
|
if (!own) printf("Owned by: UID %d\n", st.st_uid);
|
|
|
|
else
|
|
|
|
printf("Owned by: %s\n", own->pw_name);
|
2022-10-28 15:14:20 +00:00
|
|
|
printf("Mode: %s\n", mode_to_string(st.st_mode));
|
|
|
|
|
2022-10-28 19:00:33 +00:00
|
|
|
endpwent();
|
|
|
|
|
2022-10-28 15:14:20 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|