Write a line-by-line explanation of the code:
Write a line-by-line explanation of the code:
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char * errormsg[] = {
"\nUsage: ./12filestat <file|dir> [<file|dir>]\n\n", "\nFile does not exist !!\n\n", "\nError doing 'stat' on file\n\n"
};
void printerror(int msgnum, int exitcode, int exitflag);
int main(int argc, char *argv[])
{
int i;
mode_t fileperm;
struct stat filedetails;
char successmsg[] = "\nCommand executed successfully\n\n";
if ( argc < 2 )
printerror(0,2,1);
for ( i = 1; i < argc; i++ )
{
printf("\n%s\n%s\n%s\n","----------------",argv[i],"----------------");
if ( access(argv[i],F_OK) == -1 )
{
printerror(1,3,0);
continue;
}
if ( lstat(argv[i],&filedetails) < 0 )
{
printerror(2,4,0);
continue; }
if ( S_ISREG(filedetails.st_mode) )
printf("File type : Regular\n");
else if ( S_ISDIR(filedetails.st_mode) )
printf("File type : Directory\n");
else if ( S_ISLNK(filedetails.st_mode) )
printf("File type : Symbolic link\n");
else
printf("File type : Other");
printf("Number of links : %d\n", (int)filedetails.st_nlink);
printf("Time of last access : %s",ctime(&filedetails.st_atime));
printf("File Permissions:\n");
fileperm = filedetails.st_mode & ~S_IFMT;
printf("\tUser : ");
if ( fileperm & S_IRUSR ) printf("Readable, ");
else printf("Not readable, ");
if ( fileperm & S_IWUSR ) printf("Writable, ");
else printf("Not writable, ");
if ( fileperm & S_IXUSR ) printf("Executable\n");
else printf("Not executable\n");
printf("\tGroup : ");
if ( fileperm & S_IRGRP ) printf("Readable, ");
else printf("Not readable, ");
if ( fileperm & S_IWGRP ) printf("Writable, ");
else printf("Not writable, ");
if ( fileperm & S_IXGRP ) printf("Executable\n");
else printf("Not executable\n");
printf("\tOthers : ");
if ( fileperm & S_IROTH ) printf("Readable, ");
else printf("Not readable, ");
if ( fileperm & S_IWOTH ) printf("Writable, ");
else printf("Not writable, ");
if ( fileperm & S_IXOTH ) printf("Executable\n");
else printf("Not executable\n");
}
printf("%s", successmsg);
return 1;
}
void printerror(int error_index, int exit_code, int exit_flag)
{
fprintf(stderr, "%s\n",errormsg[error_index]);
if (exit_flag) exit(exit_code);
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)