Created
April 4, 2014 11:14
-
-
Save loderunner/9972470 to your computer and use it in GitHub Desktop.
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 <unistd.h> | |
#include <sys/stat.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#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)) | |
void print_access(int mask) | |
{ | |
if (mask & R_OK) | |
{ | |
printf("R_OK "); | |
} | |
if (mask & W_OK) | |
{ | |
printf("W_OK "); | |
} | |
if (mask & X_OK) | |
{ | |
printf("X_OK "); | |
} | |
printf("\n"); | |
} | |
void print_stat(mode_t mode) | |
{ | |
printf("%04o\n", mode); | |
} | |
int main(int argc, char** argv) | |
{ | |
srandomdev(); | |
// 0700 | |
print_stat(ACCESS_TO_STAT(R_OK|W_OK|X_OK, S_IRWXU)); | |
// 0666 | |
print_stat(ACCESS_TO_STAT(R_OK|W_OK, ACCESSPERMS)); | |
// 0101 | |
print_stat(ACCESS_TO_STAT(X_OK, S_IRWXU|S_IRWXO)); | |
int mask; | |
for (int i = 0; i < 10; i++) | |
{ | |
mask = random() & 07; | |
printf("access mask = "); | |
print_access(mask); | |
printf("user: "); | |
print_stat(ACCESS_TO_STAT(mask, S_IRWXU)); | |
printf("group: "); | |
print_stat(ACCESS_TO_STAT(mask, S_IRWXG)); | |
printf("other: "); | |
print_stat(ACCESS_TO_STAT(mask, S_IRWXO)); | |
printf("all: "); | |
print_stat(ACCESS_TO_STAT(mask, ACCESSPERMS)); | |
printf("\n"); | |
} | |
mode_t mode = S_IRUSR | S_IRGRP | S_IWGRP; | |
// R_OK W_OK X_OK | |
print_access(STAT_TO_ACCESS(S_IRWXU, S_IRWXU)); | |
// R_OK | |
print_access(STAT_TO_ACCESS(mode, S_IRWXU)); | |
// R_OK W_OK | |
print_access(STAT_TO_ACCESS(mode, S_IRWXG)); | |
// nothing | |
print_access(STAT_TO_ACCESS(mode, S_IRWXO)); | |
for (int i = 0; i < 10; i++) | |
{ | |
mode = random() & 0777; | |
printf("mode = "); | |
print_stat(mode); | |
printf("user: "); | |
print_access(STAT_TO_ACCESS(mode, S_IRWXU)); | |
printf("group: "); | |
print_access(STAT_TO_ACCESS(mode, S_IRWXG)); | |
printf("other: "); | |
print_access(STAT_TO_ACCESS(mode, S_IRWXO)); | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment