Question: Develop a program that accepts one command line argument, permissions in octal numbers such as 764, and outputs the permissions in letter format, (Taking the

Develop a program that accepts one command line argument, permissions in octal numbers such as 764, and outputs the permissions in letter format, (Taking the octal number and convert it to the read, write and execute permissions)

for example, rwxrw-r-- of 764. Assume the executable file is a.out. Your running demo is as below.

opus:~ $./a.out 764

rwxrw-r--

Example code)

#include #include #include #include #include #define N_BITS 3 void main(int argc, char *argv[]) { unsigned int i, mask = 0700; /*Octal Number*/ struct stat buff; static char *perm[] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"};

if (argc > 1) { if ((stat(argv[1], &buff) != -1)) { /*address op*/ printf("Permissions for %s ", argv[1]); for (i=3; i; --i) { printf("%3s", perm[(buff.st_mode & mask)>>(i-1)*N_BITS]); mask >>= N_BITS; /*right shift*/ perm[6],perm[4],perm[4] }/*mask=mask>>N_BITS;*/ putchar(' '); } else { perror(argv[1]); exit(1); } } else { fprintf(stderr, "Usage: %s file_name ", argv[0]); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!