I have a mode_t perms variable which stores the permission for a file returned from another function. I need to print the file permissions as a tring, of the form
rw-r--r-- (0644)
but what I get is
r--r--r-- (100644)
How can I get the right permissions? I've tried googling and don't understand what I'm doing wrong. Here's my code:
void print_perms(mode_t perms)
{
printf( (perms & S_IRUSR) ? "r" : "-");
printf( (perms & S_IWUSR) ? "w" : "-");
printf( (perms & S_IXUSR) ? "x" : "-");
printf( (perms & S_IRGRP) ? "r" : "-");
printf( (perms & S_IWGRP) ? "w" : "-");
printf( (perms & S_IXGRP) ? "x" : "-");
printf( (perms & S_IROTH) ? "r" : "-");
printf( (perms & S_IWOTH) ? "w" : "-");
printf( (perms & S_IXOTH) ? "x" : "-");
}
//later
print_perms(permissions);
printf(" (%d)\n", permissions);