The following code prints the permission as a number. Modify the code to print 1
ID: 3734531 • Letter: T
Question
The following code prints the permission as a number. Modify the code to print 10 lowest significant binary bits (e.g., ”0110100100”) for permission.
#include #include /* needed for stat () function */ int main(int argc, char *argv[]) struct stat fileinfo; *returned info about file/ int if (argc != 2) printf ("Usage: statfile filenameln") exit (0); i-stat (argv[1],&fileinfo;); if (i -=-1) printf ("Unable to stat %s ",argv [1]); exit (0) printf (''size : %d ",fileinfo.st-size); printf ("permissions : %d ",fileinfo.st-mode); printf (" last modified: %d ",fileinfo.st-ntíme);Explanation / Answer
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
// to find binary equivalent
void binary_equivalent(unsigned n)
{
unsigned i;
printf("Binary bits for permission is :: ");
for (i = 1 << 9; i > 0; i = i / 2) { // here initial value of i represents the no of bits in binary
// i.e for 10 bits it should be 10 - 1 = 9 so, i = 1 << 9;
(n & i)? printf("1"): printf("0");
}
}
int main(int argc, char *argv[])
{
struct stat fileinfo;
int i;
if (argc == 2)
{
i = stat(argv[1], &fileinfo);
if(i == -1)
{
printf("Unable to stat %s ", argv[1]);
exit(0);
}
printf("size : %d ", fileinfo.st_size);
printf("permission : %d ", fileinfo.st_mode);
printf("last modified: %d ", fileinfo.st_mtime);
binary_equivalent(fileinfo.st_mode);
}
else {
printf ("usage : statfile filename ");
}
return 0;
}
/*************** Output of Program ******************
size : 117
permission : 33206
last modified: 1521643692
Binary bits for permission is :: 0110110110
*******************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.