Reputation: 47
I have a quick question regarding shmget()
function in C. The program is very simple. It asks user to enter a shmid, and it out puts Key, Mode, Owner etc.
I am using buffer->shm_perm.mode
to get the permission but its giving the permissions in octal form (960 etc). Is there any way I can change it so it shows permission like "rwx"?
Thanks
Upvotes: 0
Views: 551
Reputation: 754280
A (non-reentrant) function to convert a mode_t
value into a string that might be produced by ls
:
/*
** Convert a mode field into "ls -l" type perms field.
*/
static char *lsperms(int mode)
{
static char *rwx[] = {"---", "--x", "-w-", "-wx",
"r--", "r-x", "rw-", "rwx"};
static char bits[11];
bits[0] = filetypeletter(mode);
strcpy(&bits[1], rwx[(mode >> 6) & 7]);
strcpy(&bits[4], rwx[(mode >> 3) & 7]);
strcpy(&bits[7], rwx[(mode & 7)]);
if (mode & S_ISUID)
bits[3] = (mode & 0100) ? 's' : 'S';
if (mode & S_ISGID)
bits[6] = (mode & 0010) ? 's' : 'l';
if (mode & S_ISVTX)
bits[9] = (mode & 0001) ? 't' : 'T';
bits[10] = '\0';
return(bits);
}
This can be simplified for shared memory since you don't need the file type (function not provided here anyway) or the special permissions bits.
/*
** Convert a mode field into "ls -l" type shared memory perms field.
*/
static char *shmperms(int mode)
{
static char *rwx[] = {"---", "--x", "-w-", "-wx",
"r--", "r-x", "rw-", "rwx"};
static char bits[10];
strcpy(&bits[0], rwx[(mode >> 6) & 7]);
strcpy(&bits[3], rwx[(mode >> 3) & 7]);
strcpy(&bits[6], rwx[(mode & 7)]);
bits[9] = '\0';
return(bits);
}
Upvotes: 2