Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Created November 15, 2015 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/bab0ae824bd526f17f0e to your computer and use it in GitHub Desktop.
Save zsrinivas/bab0ae824bd526f17f0e to your computer and use it in GitHub Desktop.
setItemModes
void setItemModes(const char* itemname, char* modes) {
struct stat info;
stat(itemname, &info);
mode_t mode = info.st_mode;
strcpy(modes, "----------");
if ( S_ISDIR(mode) ) modes[0] = 'd';
if ( S_ISCHR(mode) ) modes[0] = 'c';
if ( S_ISBLK(mode) ) modes[0] = 'b';
if ( mode & S_IRUSR ) modes[1] = 'r';
if ( mode & S_IWUSR ) modes[2] = 'w';
if ( mode & S_IXUSR ) modes[3] = 'x';
if ( mode & S_IRGRP ) modes[4] = 'r';
if ( mode & S_IWGRP ) modes[5] = 'w';
if ( mode & S_IXGRP ) modes[6] = 'x';
if ( mode & S_IROTH ) modes[7] = 'r';
if ( mode & S_IWOTH ) modes[8] = 'w';
if ( mode & S_IXOTH ) modes[9] = 'x';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment