C program辅导、辅导inputfile commands
- 首页 >> C/C++编程You are to write a C program that will essentially read a text file for a list of commands and will execute those commands in a pipeline:
cmd1 | cmd2 |cmd3………….| cmdn
First pass:
congaline1.c This version does not need to handle the situation where the cmd’s have arguments. It reads the textfile containing the commands from stdin. To (minimally) test your program we suggest you write 3 small programs:
•addone.c that reads (text) integers from stdin and writes those integers (incremented by one) to stdout.
•addone_in.c that opens an inputfile (“inputfile” – “hard wired” into the program) and reads (text) integers from the file and writes those integers (incremented by one) to stdout.
•addone_out.c that opens an outputfile (“outputfile” – “hard wired into the program) and reads text integers from stdin and writes those integers (incremented by one) to the opened file.
If you have written congaline1.c properly then if “inputfile” contains the number 1 and the textfile of commands contains:
./addone_in
./addone
./addone
./addone
./addone_out
then after running conglaline1 < cmdfile the output file should contain the number 6.
Second pass
congaline2.c This takes commands with arguments in the textfile. For example, the cmdfile could now be
./addone –i inputfilename
./addone
./addone
./addone
./addone
./addone
./addone –o outputfilename
(where now we have modified addone so that if it is given no argument, it expects to read from stdin and write to stdout, if it is given –i as an option, it expects a filename to take its input from and writes to stdout, if it is given –o as an option, it expects a filename to write it’s output to and reads from stdin).
NOTE: the above is just an example where we chose the first and last commands to have arguments: the program congaline.2 should not care which commands have arguments, it should simply execute the commands, with any arguments, piped together.
Hint – look at the exec family of functions, some are given the cmd to execute and then separate strings for argv[0],argv[1] etc. – others take the cmd to run and a null terminated array of string pointers for the argv[]). Choose what you like – for the purposes of the exercise you may assume no more than 10 strings in the argument list to any command. (You may also assume any cmd, together with its arguments, fits in a string of size 256 chars).
Note your code should work with any command – not just the “addone” examples It should work with – for example:
ls –l
sort –o filename
or
ls –l
sort
wc
etc
Extra challenge (not to be marked) –
write congaline3.c that allows the cmd file to optionally have redirected input at the start and redirected output at the end – eg. Valid cmd files may be:
./addone < inputfilename
./addone
./addone
./addone
./addone –o outputfilename
or
./addone –i inputfilename
./addone
./addone
./addone > outputfilename
or
./addone < inputfilename
./addone
./addone
./addone > outputfilename
or
./addone –i inputfilename
./addone
./addone
./addone
./addone –o outputfilename
Extra challenge (again not marked)
– dream up some more interesting examples that actually do something useful with standard unix commands piped together.
Submission – instructions will be posted shortly.
HINT Pseudo-code to create a set of cmds piped:
for cmd in cmds
if there is a next cmd
pipe(new_fds)
fork
if child
if there is a previous cmd
dup2(old_fds[0], 0)
close(old_fds[0])
close(old_fds[1])
if there is a next cmd
close(new_fds[0])
dup2(new_fds[1], 1)
close(new_fds[1])
exec cmd || die
else // is parent
if there is a previous cmd
close(old_fds[0])
close(old_fds[1])
if there is a next cmd
old_fds = new_fds
if there are multiple cmds
close(old_fds[0])
close(old_fds[1])
Detailed Submisison Instructions
The handin key for this exercise is: prac2. The following SVN commands will enable you to make a repository for this assignment. Please note the following:
• Perform these steps in the order written once only!
• Replace aaaaaa, where it appears in the commands, with YOUR student id.
• Some commands are long — they must be typed on one line.
Use the Unix “cd” command to change to the place where you want your exercise directory to be stored, then type these commands:
svn mkdir --parents -m "spc prac2 start"
(creates this directory in your svn tree)
(checks out a working copy in your directory) You can now begin work.
You can add a file to the repository by typing the commands:
svn add NAME-OF-FILE
svn commit -m "REASON-FOR-THE-COMMIT"
where “reason-for-the-commit” should be some brief text that explains why you changed the code since the last commit. Note that you only need to add a file once — after that, SVN will “know” it is in the repository. You are now ready to commence working on the exercise.
The files you handin must include:
1.Your C source files as specified above.
2.A Makefile that will compile your C sources as specified above.
You do not have to include any of the add_one.c files or their variants... that you used for testing we will supply those.