Update the program to display the file size, owner ID, and file permission of th
ID: 3634845 • Letter: U
Question
Update the program to display the file size, owner ID, and file permission of the file.
* Abstract: This program demonstrates the usage of stat system call.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
struct stat buf;
if (stat(argv[1], &buf) < 0 ) {
printf("Error ");
exit(-1);
}
if (S_ISREG(buf.st_mode))
{
printf("%s: regular file ", argv[1]);
}
else if (S_ISDIR(buf.st_mode))
{
printf("%s: directory ", argv[1]);
}
else
{
printf("%s: unknown type ", argv[1]);
}
return 0;
}
Explanation / Answer
[color=blue] > > CODE: > > #include > #include > #include > #include > #include > #include > #include > #include > > int main(int argc, char** argv) > { > > struct stat file; > stat(argv[1], &file); > > char path[PATH_MAX]; > > int Size = file.st_size; > int OwnerId = file.st_uid; > int GroupId = file.st_gid; > int Permissions = file.st_mode; > int LastRead = file.st_atime; > int LastWrite = file.st_mtime; > int LastChange = file.st_ctime; > > if(errno == 2) > printf("ERROR: No such file or directory "); > else if(errno == 13) > printf("Permission Denied "); > else if(errno == 20) > printf("Not a directory "); > else > { > printf(" Absolute Pathname: %s", argv[1], path); > printf(" File Size (bytes): %d", Size); > printf(" File Owner ID: %d", OwnerId); > printf(" File Group ID: %d", GroupId); > printf(" File Permissions: %d", Permissions); > printf(" Time of Last Read: %d", LastRead); > printf(" Time of Last Write: %d", LastWrite); > printf(" Time of Last Change: %d ", LastChange); > } > > return(0); > > }[/color]Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.