Created
February 20, 2017 19:43
-
-
Save bzdgn/97c140854e7e29a2b380c38ed05555fe to your computer and use it in GitHub Desktop.
Show Modes Of A Linux File
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> | |
int main(int argc, char* argv[]) | |
{ | |
if(argc != 2) { | |
printf("usage: %s <filename>\n", argv[0]); | |
exit(1); | |
} | |
struct stat fileStat; | |
int result = stat(argv[1], &fileStat); | |
if(result != 0) { | |
printf("No such file found: %s\n", argv[1]); | |
exit(2); | |
} | |
unsigned int OTH_X = 1; | |
unsigned int OTH_W = 1 << 1; | |
unsigned int OTH_R = 1 << 2; | |
unsigned int GRP_X = 1 << 3; | |
unsigned int GRP_W = 1 << 4; | |
unsigned int GRP_R = 1 << 5; | |
unsigned int USR_X = 1 << 6; | |
unsigned int USR_W = 1 << 7; | |
unsigned int USR_R = 1 << 8; | |
unsigned int MODE = fileStat.st_mode; | |
printf("File Mode: %c%c%c%c%c%c%c%c%c\n", | |
( MODE & USR_R ) == USR_R ? 'r' : '-' , | |
( MODE & USR_W ) == USR_W ? 'w' : '-' , | |
( MODE & USR_X ) == USR_X ? 'x' : '-' , | |
( MODE & GRP_R ) == GRP_R ? 'r' : '-' , | |
( MODE & GRP_W ) == GRP_W ? 'w' : '-' , | |
( MODE & GRP_X ) == GRP_X ? 'x' : '-' , | |
( MODE & OTH_R ) == OTH_R ? 'r' : '-' , | |
( MODE & OTH_W ) == OTH_W ? 'w' : '-' , | |
( MODE & OTH_X ) == OTH_X ? 'x' : '-' | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage;