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. Please download this zip file. Be sure to review the homework grading criteria. This assignment is due Tuesday, April 21.
Write a C program, called hw5.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_letter.txt
, that you can test your program with. Also in the zip file is a bash script file, test_script
, that automates the testing of your program with the sample input file (the script file demonstrates how to set environment variables and how to use I/O redirection to process files). Another text file, out_letter.txt
, contains the correct output that your program should produce when the script file is run. The included script and test files are not a very extensive test of your program. You should create better, more complete, tests.
Turn in a zip file called CS59000Hw5Surname.zip
(where Surname
is your last name) containing your C program hw5.c
that solves this problem, a Makefile
that compiles your program, and any test and/or script files that you want to include.
This assignment is due Tuesday, April 21.
Below are some references about environment variables, the getenv()
function, and I/O redirection.
export
, set
, and unset
.