Question: 1- write a C program that copies its standard input to its standard output by read(2) -ing from file descriptor 0 (until encountering an end

1- write a C program that copies its standard input to its standard output by read(2)-ing from file descriptor 0 (until encountering an end of file) and write(2)-ing to file descriptor 1. If no errors (other than EOF) are encountered, your program should exit(2) with a return code of 0.

Your program executable should accept the following (optional) command line arguments (in any combination or order):

--input=filename ... use the specified file as standard input (making it the new fd0). If you are unable to open the specified input file, report the failure (on stderr, file descriptor 2) using fprintf(3), and exit(2) with a return code of 2.

--output=filename ... create the specified file and use it as standard output (making it the new fd1). If you are unable to create the specified output file, report the failure (on stderr, file descriptor 2) using fprintf(3), and exit(2) with a return code of 3.

--segfault ... force a segmentation fault (e.g. by calling a subroutine that sets a char * pointer to NULL and then stores through the null pointer). If this argument is specified, do it immediately, and do not copy from stdin to stdout.

--catch ... use signal(2) to register a SIGSEGV handler that catches the segmentation fault, logs an error message (on stderr, file descriptor 2) and exit(2) with a return code of 4.

2- When you print out an error message (e.g. because an open failed), your message should include enough information to enable a user to understand not merely the nature of the problem but its cause ... for example:

-which argument caused the problem ... e.g. --input

-which file could not be opened ... e.g. myfile.txt

-the reason it could not be opened ... e.g. no such file

Do your argument parsing with getopt_long(3).

If you encounter an unrecognized argument you should print out an error message including a correct usage line, and exit(2) with a return code of 1.

(Suggestion :

-first process all arguments and store the results in variables

-then check which options were specified and carry actions out in the correct order:

-do any file redirection

-register the signal handler

-cause the segfault

-if no segfault was caused, copy stdin to stdout )

3- create a Makefile that supports the following targets:

-(default) ... build the lab0 executable. To maximize comile-time error checking, you should compile your program with the -Wall and -Wextra options.

-check ... runs a quick smoke-test on whether or not the program seems to work, supports the required arguments, and properly reports success or failure. Please include a brief description (in your README) of what checks you chose to include in your smoke-test.

-clean ... delete all files created by the Makefile, and return the directory to its freshly untared state.

-dist ... build the distribution tarball.

Put the code that causes the SEGFAULT in a separate subroutine.

4- run the program (with the --segfault argument) under gdb(1)

set a break-point at the bad assignment, run the program up to the breakpoint, inspect the pointer to confirm that it is indeed NULL

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!