Question
I need to combine two programs that can do the following (a) what ls command does and (b) to check each file name to print
I need to combine two programs that can do the following
(a) what ls command does and (b) to check each file name to print its content if its name has ".lst" extension.
These are the two programs
Program - 1
/* * simple-ls.c * Extremely low-power ls clone. * ./simple-ls . */ #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(1); } if ((dp = opendir(argv[1])) == NULL ) { fprintf(stderr, "can't open '%s' ", argv[1]); exit(1); } while ((dirp = readdir(dp)) != NULL ) printf("%s ", dirp->d_name); closedir(dp); return(0); }
Program - 2
/* * Stripped down version of 'cat', using unbuffered I/O. * ./simple-cat < simple-cat.c */ #include#include #include #define BUFFSIZE 32768 int main(int argc, char **argv) { int n; char buf[BUFFSIZE]; while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0) { if (write(STDOUT_FILENO, buf, n) != n) { fprintf(stderr, "write error "); exit(1); } } if (n < 0) { fprintf(stderr, "read error "); exit(1); } return(0); }
That is, you will design and implement this lscat.c program will do what simple-ls.c program does (to loop through each entry in the directory to be printed), and the program checks whether an entry is a file name and its name contains a character-pattern (".lst" at the end of the file name). If so, then it prints the file-content (that is, to open and print the content). For this part (to print the file content) the program may use the code-segment of "simple-cat" program. When the program prints the file-content, please have a user-friendly heading to show the beginning and the end of the file content being printed) as shown below (or you may have your own heading).
*** Start of the file: sample.lst ***
...
*** End of the file: sample.lst ***
Thank You
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