I need to combine two programs that can do the following (a)...

70.2K

Verified Solution

Question

Programming

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 intmain(int argc, char **argv) {    DIR *dp;    struct dirent *dirp;    if (argc != 2) {        fprintf(stderr, \"usage: %s dir_name\n\", argv[0]);        exit(1);    }    if ((dp = opendir(argv[1])) == NULL ) {        fprintf(stderr, \"can't open '%s'\n\", argv[1]);        exit(1);    }    while ((dirp = readdir(dp)) != NULL )        printf(\"%s\n\", 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 32768intmain(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\n\");            exit(1);        }    }    if (n < 0) {        fprintf(stderr, \"read error\n\");        exit(1);    }    return(0);}

That is, you will design and implement this lscat.c program willdo what simple-ls.c program does (to loop through each entry in thedirectory to be printed), and the program checks whether an entryis a file name and its name contains a character-pattern (\".lst\" atthe 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 printthe 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 theend of the file content being printed) as shown below (or you mayhave your own heading).

  *** Start of the file: sample.lst *** 
    ...
  *** End of the file: sample.lst ***
Thank You

Answer & Explanation Solved by verified expert
4.0 Ratings (601 Votes)
Here we are given with implementations of lsand cat command Using these we could make aprogram that prints out the content of any specified type of filewe need Here below I provide with a code and its explanation asthe comments of the codePROGRAM LSCATCat files with extension lstusage lscat include    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students