MCS 275 homework #5

Due date: 19971121

D. J. Bernstein

19971114

Exercise 9. Write an incl program to do preprocessor-style file inclusions. incl will print the contents of each file listed on its command line, except that it will replace any output line of the form #include "name" with the contents of the file called name.

For example, incl foo bar will print the contents of foo and bar. If the line #include "baz.h" appears in foo, it will be replaced with the contents of baz.h. If the line #include "data" appears in baz.h, it will be replaced with the contents of data. And so on.

You may assume that input lines are shorter than 1000 characters. You must check for other errors: if a file is unopenable, use perror to print a warning message with the name of the file, and exit immediately. Your program must be able to handle any number of input files—make sure to close each file when you’re done with it.

(Hint: Write a recursive function printcontents to print the contents of a file. printcontents should have one string argument giving the name of the file.)