In: Computer Science
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
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.