In this assignment you will write a C filter program that processes input text by expanding macros found in the text. The values that the macros will expand to will come from either command-line arguments or environment variables. This assignment makes use of files contained in this zip file.
Write a C program, called Hw1.c
, that reads from standard input and writes to standard output. Any character read from standard input that is not part of a macro should be written directly to standard output. When your program finds a macro in its input, it will "expand" (i.e., replace) the macro with a certain text string. In this assignment, a macro is defined to be a string of the form "#1#"
, or "#2#"
, or "#3#"
, etc., up to "#9#"
, that is, there are nine possible macros. When your program finds a macro string of the form "#n#"
(where n
is a single digit integer) and if n < argc
, then your program should write to standard output, in place of the macro string, the n'th command line argument string. If n >= argc
, then your program should use the Standard C Library function getenv()
to see if there is an environment variable named #n#
and if there is, use its string value to expand the macro. If there is no such environment variable, then your program should write the macro string unchanged to standard output.
In the zip file for this assignment you will find a sample text file, in-letter1.txt
, that you can test your program with. Also in the zip file is a Windows batch file, test-script1.cmd
, that automates the testing of your program with the sample input file (the batch file demonstrates how to set environment variables and how to use I/O redirection to process files). Another text file, out-letter1.txt
, contains the correct output that your program should produce when the batch file is run. The included test file is not a very extensive test of your program. You should create a better, more complete, test file.
Turn in a zip file called CS302Hw1Surname.zip
(where Surname
is your last name) containing your C program Hw1.c
that solves this problem and any test files that you want to include.
This assignment is due Wednesday, January 21.
Below are some references about I/O redirection, command line arguments, environment variables, and the getenv()
function.
main()
: Program Startup - MSDN