Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ * This still very simple ls with lstat and stat calls for the file information in a directory. * / #include #include #include #include

/* This still very simple ls with lstat and stat calls
for the file information in a directory.
*/
#include
#include
#include
#include
#include
#include
#include
#include
int
main(int argc, char **argv){
DIR *dp;
struct dirent *dirp;
if (argc !=2){
fprintf(stderr, "usage: %s dir_name
", argv[0]);
exit(EXIT_FAILURE);
}
if ((dp = opendir(argv[1]))== NULL ){
fprintf(stderr, "can't open '%s': %s
", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
if (chdir(argv[1])==-1){
fprintf(stderr, "can't chdir to '%s': %s
", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
while ((dirp = readdir(dp))!= NULL ){
struct stat sb;
if (stat(dirp->d_name, &sb)==-1){
fprintf(stderr, "Can't stat %s: %s
", dirp->d_name,
strerror(errno));
if (lstat(dirp->d_name, &sb)==-1){
fprintf(stderr,"Can't stat %s: %s
", dirp->d_name,
strerror(errno));
continue;
}
}
printf("%s: ", dirp->d_name);
if (S_ISREG(sb.st_mode))
printf("regular file");
else if (S_ISDIR(sb.st_mode))
printf("directory");
else if (S_ISCHR(sb.st_mode))
printf("character special");
else if (S_ISBLK(sb.st_mode))
printf("block special");
else if (S_ISFIFO(sb.st_mode))
printf("FIFO");
else if (S_ISLNK(sb.st_mode))
printf("symbolic link");
else if (S_ISSOCK(sb.st_mode))
printf("socket");
else
printf("unknown");
printf("-- with stat
");
if (lstat(dirp->d_name, &sb)==-1){
fprintf(stderr,"not for stat %s: %s
", dirp->d_name,
strerror(errno));
continue;
}
printf("%s: ", dirp->d_name);
if (S_ISREG(sb.st_mode))
printf("regular file");
else if (S_ISDIR(sb.st_mode))
printf("directory");
else if (S_ISCHR(sb.st_mode))
printf("character special");
else if (S_ISBLK(sb.st_mode))
printf("block special");
else if (S_ISFIFO(sb.st_mode))
printf("FIFO");
else if (S_ISLNK(sb.st_mode))
printf("symbolic link");
else if (S_ISSOCK(sb.st_mode))
printf("socket");
else
printf("unknown");
printf("-- with lstat
");
}
closedir(dp);
exit(EXIT_SUCCESS);
}
Part 1.30%
(a) Compile ls1.c program (attached here)
(b) Run the program (ls1) witht the following arguments
./ls1./ ; ls -l
Note here that you are running the program with ./ as an argument.
./ls1/etc > out1a.txt ; tail -30 out1a.txt; ./ls1/etc | wc
./ls1/bin > out1b.txt ; tail -30 out1b.txt; ./ls1/bin | wc
Note. When running the program, you may note a few warning or error messages (to be ignored).
Can't stat grub2.cfg: Permission denied
Can't stat extlinux.conf: No such file or directory
Part 2.70%
(a) Modify ls1.c program and name it ls2.c
(b) Update ls2.c program to do:
(1) count the file-type of each entry (file, dir, link, ...) and
(2) print total count of each file-type at the end
Sample output would be:
Reading the directory: /etc
total count for regular file: 3(which is 2% of total count)
total count for directory: 2(which is 1.5% of total count)
...
total count of the directory entries processed: 12345(which is 100%)
(c) Run the program with the following arguments and commands:
date; who | grep `whoami` ; w | grep `whoami` ; who > out2who.txt
./ls2./ ; ./ls2./> out2a.txt ; tail -30 out2a.txt ; ls -l out2a.txt
./ls2/etc > out2etc.txt ; tail -30 out2etc.txt ; wc out2etc.txt ; ls -l out2etc.txt
./ls2/bin > out2bin.txt ; tail -30 out2bin.txt ; wc out2bin.txt ; ls -l out2bin.txt
time ./ls2/etc > out3etc.txt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Real Time Database Systems Architecture And Techniques

Authors: Kam-Yiu Lam ,Tei-Wei Kuo

1st Edition

1475784023, 978-1475784022

More Books

Students also viewed these Databases questions

Question

design a simple disciplinary and grievance procedure.

Answered: 1 week ago