Task 4 (20 points) Modify myls.c so it print out additional information along wi
ID: 3774141 • Letter: T
Question
Task 4 (20 points) Modify myls.c so it print out additional information along with the file name: number of hard link, size of the file and the last modified date. You will need to use stato system call. If no path is given, list the content of the current working directory. Also your program should ignore the current working directory and the parent directory C. Your program should work like this. hbl 17euxb4 gcc Wall myls.c -o myls hbl17euxb4 nyls myls 8831 Sun Apr 17 12:36:33 2016 406 my ls.c 1 Sun Apr 17 12:35:33 2015Explanation / Answer
#include<stdio.h>
#include<dirent.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<time.h>
int main(int argc,char *argv[]){
if(argc!=2)
return -1;
char *path = argv[1];
DIR *dir = opendir(path);
if(dir == NULL){
char cwd[1024];
getcwd(cwd, sizeof(cwd));
*dir = opendir(cwd);
}
struct dirent *entry;
while((entry=readdir(dir))!= NULL){
struct stat fileStat;
if(stat(argv[1],&fileStat) < 0)
return 1;
printf("%s",argv[1]);
printf(" ");
printf("%d",fileStat.st_nlink);
printf(" ");
printf("%d",fileStat.st_size);
char filemodifieddate[10];
strftime(filemodifieddate, 10, "%c", gmtime(&(fileStat.st_ctime)));
printf("%s",filemodifieddate);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.