The following code prints the permission as number. Modify the code to print 10
ID: 3727067 • Letter: T
Question
The following code prints the permission as number. Modify the code to print 10 lowest signicant binary bits (e.g., ”0110100100”) for permission.
#include #include /* needed for tat() function */ int main(int argc, char argv]) struct stat fileinfo; / returned info about file int i; it (arge 2) printf "Usage: statfile filenameln") exit (O) i-stat (argv[1,kfileinfo); printf ("Unable to tat %s ",argv[1]); exit(0) printf ("size: %dla",fileinfo.st-size); printf ("permissions : %d ",fileinfo.st-mode); printf (" last modified: %d ",fileinfo.st_ntine);Explanation / Answer
#include <stdio.h>
#include <sys/stat.h>
int main(int argc, char *argv[]){
struct stat fileinfo;
int i;
if(argc!=2){
printf("Usage: statfile filename ");
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("File Permissions: ");
printf( (S_ISDIR(fileinfo.st_mode)) ? "1" : "0");
printf( (fileinfo.st_mode & S_IRUSR) ? "1" : "0");
printf( (fileinfo.st_mode & S_IWUSR) ? "1" : "0");
printf( (fileinfo.st_mode & S_IXUSR) ? "1" : "0");
printf( (fileinfo.st_mode & S_IRGRP) ? "1" : "0");
printf( (fileinfo.st_mode & S_IWGRP) ? "1" : "0");
printf( (fileinfo.st_mode & S_IXGRP) ? "1" : "0");
printf( (fileinfo.st_mode & S_IROTH) ? "1" : "0");
printf( (fileinfo.st_mode & S_IWOTH) ? "1" : "0");
printf( (fileinfo.st_mode & S_IXOTH) ? "1" : "0");
printf(" ");
printf("last modified %d ",fileinfo.st_mtime);
}
}
Please respond in comment section if answer needs an improvement. Rate it
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.