Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Created February 20, 2017 19:43
Show Gist options
  • Save bzdgn/97c140854e7e29a2b380c38ed05555fe to your computer and use it in GitHub Desktop.
Save bzdgn/97c140854e7e29a2b380c38ed05555fe to your computer and use it in GitHub Desktop.
Show Modes Of A Linux File
#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' : '-'
);
}
@bzdgn
Copy link
Author

bzdgn commented Feb 20, 2017

Usage;

showmodes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment