This assignment will be done in Unix only. It will involve input and output redirection and piping information from one process to another.
Write a program that takes any number of arguments, but the number of arguments must be an even number. Each pair of arguments will be a request to replace any instance of the first member of the pair with the second member of the pair in an input file
If the first member of the pair is the keyword in, the second member of the pair is the name of an input file. If the first member of a pair is the keyword out, the second argument is the name of an output file. There must be an input file, but there does not need to be an output file. If there is no output file, the output is written to standard output.
For example, suppose the file testfile contains the string The quick brown fox jumps over the lazy dog.
If you entered the command:
a.out fox chicken in testfile
your program would read the file testfile and replace
all instances of the string fox with the
string chicken, so it would write this on standard output.
The quick brown chicken jumps over the lazy dog.
If there is more than one replacement requested, they
will be done sequentially For example, the command
a.out dog retriever retriever golden-retriever in testfile out outfile
would read in the file testfile, replace any instance of dog with
retriever and then replace any instance of retriever with
golden-retriever, so the contents of outfile would be
The quick brown fox jumps over the lazy golden-retriever.
I have done the hard work for you. I have written a
program called replacestring.c which you must
use. This program takes exactly two arguments, which
represent one replacement pair. It reads from
standard input and writes to standard output.You should compile this
to an executable called replacestring, and call this
once (using fork and exec) for each argument pair, piping and redirecting input
and output as required. For example, if the command line
was
a.out fox chicken dog retriever in infile out outfile
your program would call fork twice, and in each case the child
would exec replacestring. The first child would read from the file
infile and process with the arguments fox and chicken, sending the
output to a pipe. The second child would read from the pipe,
process with the arguments dog and retriever, and send the output
to the file outfile.
If you define _DEBUG_ with the -D option
during the compile of replacestring.c
(gcc -D _DEBUG_ -o replacestring replacestring.c)
it will display potentially helpful information about its progress as
it runs.
You cannot incorporate any of the code from replacestring.c into your program; you must exec the replacestring process to do the work.
Note that your program must not leave zombie processes around. After
running your program, execute the command
ps -fe | grep defunct
from the shell. If
there are defunct processes with your user id, you will lose points.
The in and out arguments to your program can appear anywhere in the argument list. They do not need to be the last arguments.
You do not need to understand how replacestring.c works; you
just have to use it. Note that you can experiment with
it by using the pipeline, input redirection and output redirection
features of the shell. For example
replacestring fox chicken < testfile | replacestring chick duck
You must follow the program guidelines provided for the first project.
Grading
| Compiles and is a sincere effort | 40% |
| Handles input and output files correctly | 10% |
| Handles the case with one fork correctly | 10% |
| Handles the case with two forks correctly | 10% |
| Handles a case with three forks correctly | 10% |
| Handles any number of argument pairs (4+) correctly | 10% |
| Documentation and program design | 10% |
This project is due at 11:59PM on Tuesday, Oct. 7.