Question

In: Computer Science

I need to combine two programs that can do the following (a) what ls command does...

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 <sys/types.h>

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>

int
main(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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#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\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 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

Solutions

Expert Solution

Here, we are given with implementations of ls and cat command. Using these we could make a program that prints out the content of any specified type of file we need. Here below I provide with a code, and its explanation as the comments of the code.

PROGRAM (LS-CAT)

/*

Cat files with extension .lst

usage ./lscat <directory>
*/

#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define BUFFSIZE 32768

int catfile(char filename[]) {
int n;
// char buf[BUFFSIZE];
   //printf("%s",filename);

   FILE *f = fopen(filename, "rb");
   fseek(f, 0, SEEK_END); //It moves file pointer position to the end of the file.
   long fsize = ftell(f); // To know number of characters in the file
   fseek(f, 0, SEEK_SET); // It moves file pointer position to the beginning of the file.

   char *buf = malloc(fsize + 1);
   fread(buf, fsize, 1, f); //reads the contents of the file into buf
   fclose(f);

   buf[fsize] = '\0'; //this adds Null character at the end of buf
   int i=0;
   printf("\n*** BEGINNING OF THE FILE %s:\n",filename);
   while(buf[i]!='\0'){
       printf("%c",buf[i]); //prints the contents of the file untl we reach null character
       i++;  
   }
   printf("*** END OF THE FILE %s:\n",filename);
return(0);
}

int main(int argc, char **argv) {

DIR *dp;
struct dirent *dirp;

if (argc != 2) {
fprintf(stderr, "usage: %s dir_name\n", argv[0]); //prints the usage.
exit(1);
}

if ((dp = opendir(argv[1])) == NULL ) {
fprintf(stderr, "can't open '%s'\n", argv[1]);
exit(1);
}

while ((dirp = readdir(dp)) != NULL ){
   char * point;
       if((point = strrchr(dirp->d_name,'.')) != NULL ) { // strrchr gives the pointer point to the character '.'
           if(strcmp(point,".lst") == 0) { //Checks the extension of the file
               printf("%s\n", dirp->d_name); //Prints the filename if it ends with extension .lst
               catfile(dirp->d_name); // Function call to catfile function to print the contents of the file.
           }
       }
   }
closedir(dp);
return(0);
}

END OF PROGRAM (LS-CAT)

Here is the sample output.


Related Solutions

a) Observe the following Linux commands and work out what each command does: 1) $ ls...
a) Observe the following Linux commands and work out what each command does: 1) $ ls –al 2) $ /etc/profile 3) $ chmod 200 final_assignment.txt b) Consider a scenario where Bob has downloaded his messages to the local machine with an access of POP3, he can create mail folders and move the downloaded messages into the folders. Bob can then delete messages, move messages across folders, and search for messages (by sender name or subject). But this paradigm—namely, folders and...
what do smaller things need to combine to larger things, and what role does temperature play...
what do smaller things need to combine to larger things, and what role does temperature play in that?
2. Does the market solution need either tradition and/or command? And does tradition and command solutions...
2. Does the market solution need either tradition and/or command? And does tradition and command solutions need markets?
What does the ​ netstat command do? The netstat command has many options. Please explain the...
What does the ​ netstat command do? The netstat command has many options. Please explain the following commonly used options and briefly explain the meaning of the output. How can you know which program (i.e., process) is using a specific port? ~$​ netstat -a ~$ ​ netstat -l ~$ ​ netstat -s ~$ ​ netstat -p ~$ ​ netstat -r ~$ ​ netstat -ie You can ignore those lines related to UNIX domain sockets.
Part I– Check your file system Step 1: Execute the ls /dev/sd* command to see the...
Part I– Check your file system Step 1: Execute the ls /dev/sd* command to see the current hard disk devices. Step 2. Execute the fdisk -l command to list the current hard disk partitions Step 3. Execute the parted -l command to list the current hard disk partition table. Part II– Create a new virtual disk Step 1. In the Oracle VM VirtualBox Manager program, create a new virtual hard disk with the size of 200 MB, and name it...
Question 3: What do the following vim commands do in command mode? ZZ :w! i dd...
Question 3: What do the following vim commands do in command mode? ZZ :w! i dd x yy p Question 4: Review the following code. If you were to print out all the dereferenced values of the three pointer variables and the array, what is the output? (note: if possible explain how the output changed.) int array[] = { 30, 40, 50, 60, 70, 80 }; int *ptrA = array; int *ptrB = ptrA + 4; int *ptrC = ptrB--;...
What types of programs do released prisoners need in order to be successful?
What types of programs do released prisoners need in order to be successful?
I need you to do both of the programs. Please specify which program is which! Array...
I need you to do both of the programs. Please specify which program is which! Array Allocator: Write a function that dynamically allocates an array of integers. The function should accept an integer argument indicating the number of elements to allocate. The function should return a pointer to the array. Call the function in a complete program. Reverse Array: Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of...
How do I input this in R studio 1. Combine the following lists into a matrix...
How do I input this in R studio 1. Combine the following lists into a matrix called animals, where each list becomes a row: Cats = 2,2,4 Dogs = 1,2,1 Cows = 30,35,41 2. Add column names of “Farm A”, Farm B”, and “Farm C”. 3. Transpose the rows and columns in animals and store the result to a matrix called farms. 4. Display the contents of farms.
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT