Question: 1 Subprogram Tee ( 1 0 0 pts ) Using C , write a program subprogramtee.c that does the following: The program starts by checking

1 Subprogram Tee (100 pts)
Using C, write a program subprogramtee.c that does the following:
The program starts by checking if it got at least 3 command line arguments, including the 0-th argument, which is its own name1
. If less command line arguments are provided, the program prints an
error message on standard error and exits with status code 1. See below for details.
The program then opens its 1-st command line argument argv[1] as a file for writing, using
int open(const char *pathname, int flags, mode_t mode);. If this operation
fails, the program prints an error message on standard error and exits with status code 1. See below
for examples. The program opens the file with file mode 0644.
The program then creates an unnamed pipe using pipe(). If that operation fails, it closes the file
again, prints an error message on standard error and exits with status code 1.
The program then forks of a child process. If the operation fails, the program closes the file and both
ends of the pipe. It prints an error message on standard error and exits with status code 1.
The child process then closes the read end of the pipe as well as the file. It closes its standard output
and dups the write end of the pipe to become its new standard output, using dup2(). It then closes
the write end of the pipe. If any of these operations fail, the child process prints an error message on
standard error and exits with status code 2.
The child process then replaces itself with a new executable, whose name is given by the 2-nd command line argument argv[2]. That new executables command line arguments are given by the
3-rd,4-th etc. command line arguments, if applicable2
. The child uses execvp() to replace itself
with a new executable. You may assume that argc is always less than 8192.
After the fork, the parent process closes the write end of the pipe. If this operation fails, it prints
an error message on standard error but it continues as if no error happened. It then reads using
read() from the read end of the pipe. It stops reading when End-Of-File is hit. It writes everything
it reads into both the file and onto standard output. It uses write() for these operations. You will
want to wrap write() into a function of your own that calls write() as many times as needed
until all bytes that need to be written have been written3
.
When the parent process hits End-Of-File on the pipe, it just waits on its child to die, using wait()
or waitpid(). When the wait returns, the parent closes the file, possibly with an error message if
the close operation fails, and then exits with status code 0 if everything went fine.
The overall functionality of the program is the following: the program runs the executable whose name is
passed in the 2-nd command line argument. Everything that executable produces on its standard output is
displayed on standard output and in the file whose name is passed in the 1-st command line argument.
You are not allowed to use any functions besides the layer-2 functions cited above, as well as fprintf()
on stderr and strerror()(as well as errno) for error messaging.
Here are a couple of examples for the execution of this program:
$ ./subprogrammtee
Not enough command line arguments.
$ ./subprogrammtee test.txt
Not enough command line arguments.
$ rm test.txt
rm: cannot remove test.txt: No such file or directory
$ touch test.txt
$ cat test.txt
$ ./subprogrammtee test.txt echo Hello World "Hola el mundo"
Hello World Hola el mundo
$ cat test.txt
Hello World Hola el mundo
$ rm test.txt
$ ./subprogrammtee test.txt ls
subprogrammtee
subprogrammtee.c
test.txt
$ cat test.txt
subprogrammtee
subprogrammtee.c
test.txt
$ ./subprogrammtee /test.txt cat
Cannot open file "/test.txt": Permission denied
$ echo $?
1
$ ./subprogrammtee test.txt cat << EOF
> Hello World
> How are you?
> EOF
Hello World
How are you?

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!