Homework 4

This homework is worth 15 points.

Goals

When you finish this homework you should:

Assignment

Please note, there was an error in the spec, please used -x to set the high value and -h for help.

There are times when it is nice to have a program to produce a string of input. See for example the program yes. For this program you will produce a utility which will supply a numeric sequence. Please call your program counter.

Counter will be controlled by command line arguments, environment variables or the default behavior. If no control is specified the default behavior occurs, an environment variable setting will take precedence over the default behavior and a command line argument will take precedence over an environment variable.

The default behavior of counter is to print integer values, starting at 1 and incriminating by 1 each time. When the value becomes INT_MAX (see /usr/include/limits.h), the program should exit. By default the output will be in base 10.

The range can be changed by the command line argument --low n or -l n where n is an integer. For example counter --low 3 will start counting from three. The last number to output can be controlled by the command line arguments --high n or -x n. So the command counter -l 3 -x 7 would count from three to seven inclusive. The user can also control these ranges with the environment variable COUNTER_HIGH and COUNTER_LOW. Note, if the high value is lower than the low value, then the program will count backwards. So counter --high 3 --low 7 would count from 7 down to 3. If the high and low values are the same, the program should print that value and exit.

The range can also be set with a command line argument --range l h or -r l h where l and h are integers. This sets both the low and high values for the program, so counter -r 1 3 should print the numbers 1 through 3 inclusive.

The step for counting defaults to 1, but can be changed to any positive integer with the command line argument --step n or -s n. It can also be overridden with the environment variable COUNTER_STEP. The command counter -s 4 -l 0 -x 8 should produce the output 0, 4, and 8. The output should never exceed the high value, so counter --low 0 --high 7 --step 4 should print only a 0 and 4.

Your program should be able to output different delimiters between the numbers. By default the delimiter should be the newline character, but any valid ascii character can be used. This is controlled by the environment variable COUNTER_DELIM or the command line argument -d c or --delim c. counter --low 1 -x 7 -s 2 -d , should produce 1,3,5,7. You may focus your work on single characters if you wish, do not be concerned about escaped characters. You should not print a delimiter after the last value output, but you should always print a newline before your program exits.

Finally, output in different bases should be supported. The command line arguments --base n or -b n or the environment variable COUNTER_BASE allow the user to select any base between 2 and 16. The command counter -l 2 -x 8 -b 2 --delim , -s 2 should produce 10,100,110,1000.

Your program should support a help feature. If it is run with the command line argument -h, ? or --help, your program should print a help message (similar to ls --help) and exit. This is normally accomplished through a user supplied routine called usage().

If your program is given unknown, incomplete, or illegal instructions it should inform the user of the error, call usage() function and exit with an appropriate exit code. Supported error codes include
Value Error
0 Normal exit
1 Illegal base, outside of range
2 Unknown argument
3 Incomplete argument

Discussion

Submission

Email a copy of the C code to complete the final task to your instructor by class time. If your code consists of a single file, the source code will be sufficient, if the code is in multiple files, you should submit a tar file containing the source code and a make configuration file required to build the project.