Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please, do this in Linux using c programming, Both task are linked together so h

ID: 3891380 • Letter: P

Question

Please, do this in Linux using c programming, Both task are linked together so have to match somewhat. Thanks

Task 1 Implement a program to display a file's contents. It should be similar to the UNIX cat command when given a single file. Name your command show. It should have the following syntax: S/show fileName The above program displays the contents of the file named fileName Each system call must have error checking, and print an error message if it occurs You must use open, read, write and close system calls Task 2- modify task2 Modify the above program to implement a program called body similar to UNIX commands head or tail. It should print from the mh line to the nh line of the given file. It should have the following syntax: s /body fileName The above program displays the contents of the file named fileName, from line m to line n Each system call must have error checking, and print an error message if it occurs You must use open , read), write), and close system calls.

Explanation / Answer

cat

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

int main(int argc, char **argv){
    char    readbuf[1];
    int     fp1,fp2;
    int num_bytes;
    char str[20];

    if (argc != 2){
        perror("Usage ./cat <filename> ");
        return 0;
    }
    fp1 = open(argv[1],O_RDONLY);
    if (fp1 < 0){
       perror("Error opening file ");
    }
    num_bytes = 1;
    int count = 1;
    while (num_bytes > 0){
        if (readbuf[0] == ' ')
           count++;
        num_bytes = read(fp1, readbuf, sizeof(readbuf));
        write(1,readbuf,num_bytes);
    }
    close(fp1);
    exit(0);
}

body

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


void tostring(char str[], int num){
   int i, rem, len = 0,n;
   n = num;
   while(n!=0){
     len++;
     n/= 10;
   }

   for (i = 0; i<len; i++){
       rem = num % 10;
       num = num /10;
       str[len-(i+1)] = rem + '0';
   }
   str[len] = '';
}

int main(int argc, char **argv){
    char    readbuf[1];
    int     fp1,fp2;
    int num_bytes;
    char str[20];

    if (argc != 4){
        perror("Usage ./body m n <filename> ");
        return 0;
    }
    int m = atoi(argv[1]);
    int n = atoi(argv[2]);

    if (m == 0 || n == 0){
        perror("Invalid arguments ");
        return 0;
    }   
    fp1 = open(argv[3],O_RDONLY);
    if (fp1 < 0){
       perror("Error opening file ");
       return 0;
    }
    num_bytes = 1;
    int count = 1;
    while (num_bytes > 0){
        if (readbuf[0] == ' ')
           count++;
        num_bytes = read(fp1, readbuf, sizeof(readbuf));
        if (count >= m && count <= n)
           write(1,readbuf,num_bytes);
    }
    close(fp1);
    exit(0);
}