Question: Show.c: // Include Block #include #include // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I

Show.c:
// Include Block #include
// Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/
// main function int main(int argc, char *argv[]) { // Variable declarations int fd; // file descripter char buffer[BUF_SIZE]; // string for buffer between read and write long n; // amount read by read()
if (argc != 2) { write(STDOUT_FILENO, "Usage: show filename ", 21); return 1; }
// Open the file given as an argument by the user if ((fd = open(argv[1], O_RDONLY)) == -1 ) { // Report error opening write(STDERR_FILENO, "Error opening file. ", 20); return 1; }
// Loop through until nothing is read while ( (n = read(fd, buffer, BUF_SIZE)) > 0 ) { // Write what was read to the stdout if( write(STDOUT_FILENO, buffer, n) != n ) { //report error writing write(STDERR_FILENO, "Error writing character. ", 25); return 3; } }
// Error check if(n == -1) { // report error reading write(STDERR_FILENO, "Error reading character. ", 24); return 2; }
/* If we got here that means n=0, meaning it successfully read and wrote the whole file without error, and there is nothing left to read. */
// Close the file close(fd);
// Success: return 0; }
Testfile:
1: This 2: is 3: a 4: 5: test 6: file 7: with 8: a 9: bunch 10: of 11: lines 12: for testing out lots of code. I want to have many many many many words on this line just because I feel like it. Hopefully it prints correctly. 13: And here is the final lines. 14: 15: Fin!
***Please complete this task using the show.c file and testfile provided. You must edit the show.c file***
Tasks Modify the program from your In-Class Lab 03 to implement a program called body similar to UNIX commands head or tail. It should print from the mth line to the nth line of the given file. It should have the following syntax: $ ./ body m n 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. 20 points each
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
