Question: 1 Subprogram Tee ( 1 0 0 pts ) Using C , write a program subprogramtee.c that does the following: The program starts by checking
Subprogram Tee pts
Using C write a program subprogramtee.c that does the following:
The program starts by checking if it got at least command line arguments, including the th argument, which is its own name
If less command line arguments are provided, the program prints an
error message on standard error and exits with status code See below for details.
The program then opens its st command line argument argv as a file for writing, using
int openconst char pathname int flags, modet mode; If this operation
fails, the program prints an error message on standard error and exits with status code See below
for examples. The program opens the file with file mode
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
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
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 dup 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
The child process then replaces itself with a new executable, whose name is given by the nd command line argument argv That new executables command line arguments are given by the
rdth etc. command line arguments, if applicable
The child uses execvp to replace itself
with a new executable. You may assume that argc is always less than
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 EndOfFile 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 written
When the parent process hits EndOfFile 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 if everything went fine.
The overall functionality of the program is the following: the program runs the executable whose name is
passed in the 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 st command line argument.
You are not allowed to use any functions besides the layer functions cited above, as well as fprintf
on stderr and strerroras 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 testtxt: 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 testtxt cat
Cannot open file testtxt: Permission denied
$ echo $
$ 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
