Write a line-by-line explanation of the code:

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter2: Using Data
Section: Chapter Questions
Problem 16RQ
icon
Related questions
Question

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);
}

 

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT