Skip to content

Instantly share code, notes, and snippets.

@loderunner
Last active August 29, 2015 13:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loderunner/9972280 to your computer and use it in GitHub Desktop.
Save loderunner/9972280 to your computer and use it in GitHub Desktop.
#define S_IRUGO (S_IRUSR|S_IRGRP|S_IROTH)
#define S_IWUGO (S_IWUSR|S_IWGRP|S_IWOTH)
#define S_IXUGO (S_IXUSR|S_IXGRP|S_IXOTH)
#define ACCESS_TO_STAT(mask, user_mask) (((((mask)&R_OK)?S_IRUGO:0)|(((mask)&W_OK)?S_IWUGO:0)|(((mask)&X_OK)?S_IXUGO:0))&(user_mask))
#define STAT_TO_ACCESS(mode, user_mask) ((((mode)&(user_mask)&S_IRUGO)?R_OK:0)|(((mode)&(user_mask)&S_IWUGO)?W_OK:0)|(((mode)&(user_mask)&S_IXUGO)?X_OK:0))
// Usage: ACCESS_TO_STAT(R_OK | W_OK | X_OK, S_IRWXU) => 0700
// ACCESS_TO_STAT(R_OK | W_OK, ACCESSPERMS) => 0666
// ACCESS_TO_STAT(X_OK, S_IRWXU|S_IRWXO) => 0101
// STAT_TO_ACCESS(0444, S_IRWXU) => R_OK
// STAT_TO_ACCESS(0444, S_IRWXG) => R_OK | W_OK
// STAT_TO_ACCESS(0444, S_IRWXO) => 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment