Please Do this in Linux. Task 1 . (50 points) Implement your own version of a co
ID: 3870469 • Letter: P
Question
Please Do this in Linux.
Task 1. (50 points)
Implement your own version of a command to display a file that should be almost same as the UNIX cat command that displays a requested file on the screen. Name your command as "show" that should have the following syntax:
show file_name
that displays the file called file_name and reports with an appropriate message if the file name does not exist or can’t be opened.
You need to create a text file name "file1" for at least 30 continuous character for test use. Meanwhile, you need to upload the snapshot of the demo of your program in console of Linux. Only zip the file to be .zip. That is in the folder Lab 3, you only need to have 5 files after compile: show.c body.c show body file1 snapshot(.jpg,.bmp....). (body is the one for task 2)
Task 2. (50 points)
Modify the above program to implement your own version of UNIX command called body like UNIX commands head or tail. Your command body should print from mth line to nth line from the given file. It should have the following syntax:
body m n file_name
The command reports with an appropriate message if the file name does not exist or can’t be opened. If you don't know how to do the mth line to nth line to get the full scores, you can do the mth character to nth character instead, with 80% of the score.
You need to create a text file name "file1" for at least 10 lines for test use. Meanwhile, you need to upload the snapshot of the demo of your program in console of Linux. Only zip the file to be .zip. That is in the folder Lab 3, you only need to have 5 files after compile: show.c body.c show body file1 snapshot(.jpg,.bmp....).
Thank You :)
Explanation / Answer
// Task 1.
#include<stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
char line[2048];
int i,k=1;
if(argc < 2 )
{
printf("Filename is missing .... ");
printf("Usage : ");
printf(" show filename ");
return 1;
}
fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("%s does not exist or can’t be opened ", argv[1]);
return 2;
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF) break;
if (ch!=' ')
line[i++]=ch;
else
{
line[i]=0;
printf("Line-%d : %s ", k++,line);
i=0;
}
}
fclose(fp);
return 0;
}
/*
lenovo@lenovo-Vbox:~/chegg$ cc show.c -o show
lenovo@lenovo-Vbox:~/chegg$ ls
1to100.c call_data.txt~ ccmplx dtob.c fib.c hello.c lab6.c numbers.txt show.c tstcpp.cpp
1to100.c~ call_stats3 cmplx dtob.c~ fib.c~ helloc~ lab6.c~ readseq.cpp show.c~ tstcpp.cpp~
a.out call_stats3.cpp cmplx.cpp dtob.s furits lab5.c main.c readseq.cpp~ simu.c weekly_call_info.txt
call_data.txt call_stats3.cpp~ cmplx.cpp~ err.txt helloc lab5.c~ main.c~ show simu.c~
lenovo@lenovo-Vbox:~/chegg$ ./show
Filename is missing ....
Usage :
show filename
lenovo@lenovo-Vbox:~/chegg$ ./show fib.c
Line-1 : #include<stdio.h>
Line-2 :
Line-3 : int i=2;
Line-4 : void fibo( int *a)
Line-5 : {
Line-6 : int x=i;
Line-7 : a[x]= a[x-2]+a[x-1];
Line-8 : }
Line-9 : int main()
Line-10 : {
Line-11 : const int n=25;
Line-12 : int fib[n];
Line-13 :
Line-14 : fib[0]=0;
Line-15 : fib[1]=1;
Line-16 :
Line-17 : for(;i<n;i++)
Line-18 : {
Line-19 : fibo(fib);
Line-20 : }
Line-21 :
Line-22 : for(i=0;i<n;i++)
Line-23 : printf(" %d ", fib[i]);
Line-24 :
Line-25 : printf(" ");
Line-26 : return 0;
Line-27 : }
lenovo@lenovo-Vbox:~/chegg$
*/
// Task-2
#include<stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
char line[2048];
int i,k=1,m,n;
if(argc < 4 )
{
printf("Missing necessary parameters ... ");
printf("Usage : ");
printf(" body m n filename ");
return 1;
}
m=atoi(argv[1]);
n=atoi(argv[2]);
fp=fopen(argv[3],"r");
if(fp==NULL)
{
printf("%s does not exist or can’t be opened ", argv[1]);
return 2;
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF) break;
if (ch!=' ')
line[i++]=ch;
else
{
line[i]=0;
i=0;
if(k>=m && k<=n)
printf("Line-%d : %s ", k,line);
k++;
}
}
fclose(fp);
return 0;
}
/*
lenovo@lenovo-Vbox:~/chegg$ ./body
Missing necessary parameters ...
Usage :
body m n filename
lenovo@lenovo-Vbox:~/chegg$ ./body 3
Missing necessary parameters ...
Usage :
body m n filename
lenovo@lenovo-Vbox:~/chegg$ ./body 3 6
Missing necessary parameters ...
Usage :
body m n filename
lenovo@lenovo-Vbox:~/chegg$ ./body 3 6 fruits
Line-3 : orange
Line-4 : purple
Line-5 : banana
Line-6 : sugarcane
lenovo@lenovo-Vbox:~/chegg$ cat fruits
mango
apple
orange
purple
banana
sugarcane
water
coac
pepsi
mazza
lenovo@lenovo-Vbox:~/chegg$
*/