Created
February 20, 2017 21:06
-
-
Save bzdgn/79700d8a4a8d93614fe96374ab7f5a83 to your computer and use it in GitHub Desktop.
Custom Stat Printing The Parameters Of A File (Code is not mine, check out Chris Brown's System Programming lessons)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/stat.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(int argc, char * argv[]) | |
{ | |
struct stat sb; | |
char *filetype[] = { | |
"unknown", "FIFO", "character device", "unknown", "directory", | |
"unknown", "block device", "unknown", "file", "unknown", | |
"symlink", "unknown", "socket" | |
}; | |
if (argc != 2) { | |
fprintf(stderr, "usage: listfile filename\n"); | |
exit(1); | |
} | |
if ( stat(argv[1], &sb) < 0 ) { | |
perror(argv[1]); | |
exit(2); | |
} | |
printf("file type: %s\n", filetype[ (sb.st_mode >> 12) & 017 ]); | |
printf("permissions: %c%c%c%c%c%c%c%c%c\n", | |
(sb.st_mode & S_IRUSR) ? 'r' : '-', | |
(sb.st_mode & S_IWUSR) ? 'w' : '-', | |
(sb.st_mode & S_IXUSR) ? 'x' : '-', | |
(sb.st_mode & S_IRGRP) ? 'r' : '-', | |
(sb.st_mode & S_IWGRP) ? 'w' : '-', | |
(sb.st_mode & S_IXGRP) ? 'x' : '-', | |
(sb.st_mode & S_IROTH) ? 'r' : '-', | |
(sb.st_mode & S_IWOTH) ? 'w' : '-', | |
(sb.st_mode & S_IXOTH) ? 'x' : '-' | |
); | |
printf("last accessed: %s", ctime(&sb.st_atime)); | |
printf("last modified: %s", ctime(&sb.st_mtime)); | |
printf("last change : %s", ctime(&sb.st_ctime)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check Out Chris Brown's awesome System Programming lessons
Usage;