Answered step by step
Verified Expert Solution
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
mainint argc, char argv
DIR dp;
struct dirent dirp;
if argc
fprintfstderr "usage: s dirname
argv;
exitEXITFAILURE;
if dp opendirargv NULL
fprintfstderr "can't open s: s
argv strerrorerrno;
exitEXITFAILURE;
if chdirargv
fprintfstderr "can't chdir to s: s
argv strerrorerrno;
exitEXITFAILURE;
while dirp readdirdp NULL
struct stat sb;
if statdirpdname, &sb
fprintfstderr "Can't stat s: s
dirpdname,
strerrorerrno;
if lstatdirpdname, &sb
fprintfstderr"Can't stat s: s
dirpdname,
strerrorerrno;
continue;
printfs: dirpdname;
if SISREGsbstmode
printfregular file";
else if SISDIRsbstmode
printfdirectory;
else if SISCHRsbstmode
printfcharacter special";
else if SISBLKsbstmode
printfblock special";
else if SISFIFOsbstmode
printfFIFO;
else if SISLNKsbstmode
printfsymbolic link";
else if SISSOCKsbstmode
printfsocket;
else
printfunknown;
printf with stat
;
if lstatdirpdname, &sb
fprintfstderr"not for stat s: s
dirpdname,
strerrorerrno;
continue;
printfs: dirpdname;
if SISREGsbstmode
printfregular file";
else if SISDIRsbstmode
printfdirectory;
else if SISCHRsbstmode
printfcharacter special";
else if SISBLKsbstmode
printfblock special";
else if SISFIFOsbstmode
printfFIFO;
else if SISLNKsbstmode
printfsymbolic link";
else if SISSOCKsbstmode
printfsocket;
else
printfunknown;
printf with lstat
;
closedirdp;
exitEXITSUCCESS;
Part
a Compile lsc program attached here
b Run the program ls witht the following arguments
ls ; ls l
Note here that you are running the program with as an argument.
lsetc outatxt ; tail outatxt; lsetc wc
lsbin outbtxt ; tail outbtxt; lsbin wc
Note. When running the program, you may note a few warning or error messages to be ignored
Can't stat grubcfg: Permission denied
Can't stat extlinux.conf: No such file or directory
Part
a Modify lsc program and name it lsc
b Update lsc program to do:
count the filetype of each entry file dir, link, and
print total count of each filetype at the end
Sample output would be:
Reading the directory: etc
total count for regular file: which is of total count
total count for directory: which is of total count
total count of the directory entries processed: which is
c Run the program with the following arguments and commands:
date; who grep whoami ; w grep whoami ; who outwho.txt
ls ; ls outatxt ; tail outatxt ; ls l outatxt
lsetc outetc.txt ; tail outetc.txt ; wc outetc.txt ; ls l outetc.txt
lsbin outbin.txt ; tail outbin.txt ; wc outbin.txt ; ls l outbin.txt
time lsetc outetc.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started