#include #include #include 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; }