Try Firefox!

IC221 Lab 4: Applying the Unix Philosophy

Summary
This lab (which is small enough for a single period) asks you to create a small program following the philosophy of unix utilities. By doing so, you'll create a program that future users can use as flexibly as our old friends grep, wc, cat, etc.

Lab setup
  1. Create a directory lab04 for this lab
  2. Copy this compressed archive file to the above directory:
    ~wcbrown/courses/IC221/labs/L04/lab04.tar.gz
  3. Unzip and untar the archive.
  4. *** Take note of all the files that are part of this archive ***
  5. *** Read this entire lab assignment BEFORE you start on Part 1 ***

Part 1 (50 points): Create your own shell utility
Write a C source code file named mma.c and compile from it an executable program named mma. The program computes the minimum, maximum, and average of the the real numbers it reads from stdin. Your program may not assume there are a particular number of input values: it must work for ANY number of values. Use tabs to separate output values, and print results to three decimal places. Here's the expected beahvior and output for the input file data.txt:
chessie [80] [~/]> mma < data.txt		
2.350   999.268 497.530
chessie [81] [~/]>
(Recall that we're using the shell here to do input redirection: you're writing mma so it reads from stdin, but the shell arranges for data.txt to be the actual source of the data) Note that we could have accomplished the same thing, this way:
chessie [82] [~/]> cat data.txt | mma	
2.350   999.268 497.530
chessie [83] [~/]>
(Recall that we're using the shell here to pipe stdout of cat to stdin of mma) Here's the expected behavior for an input file that has no data (i.e., is empty):
    chessie [84] [~/]> mma < empty.txt
    mma: Error - empty input, min/max/average undefined!
    chessie [85] [~/]> echo $?
    1
Your program should follow the Unix conventions we've discussed so far:
  1. mma reads from stdin and writes to stdout.
  2. mma does not interact at all with the user (i.e., do not prompt the user for anything).
  3. Do not put unnecessary formatting in the output. Just output the results, nothing more, nothing less (i.e., no extra text, etc.)
  4. Do not "die silently": if an error occurs be vocal about it (give a helpful error message). Error messages go to stdout, and a non-zero return value indicates an error.

Part 2 (30 points): Add command line options
Augment mma.c to add the four command line options: --min, --max, --ave and --help. If one or more of the first options is present on the command line, the corresponding result is printed. If the last option (i.e. --help) is given, the program should print out a description of what the program does and how to use it, then exit immediately. The exact message is
mma [--min] [--max] [--ave] [--help]
Here's the expected behavior and output for the input file data.txt:
    chessie [80] [~stahl]> mma --min < data.txt
    2.350
    chessie [80] [~stahl]> mma --max < data.txt
    999.268
    chessie [80] [~stahl]> mma --max --ave < data.txt
    999.268 497.530
    chessie [80] [~stahl]> mma --max --max < data.txt
    999.268 999.268
    chessie [80] [~stahl]> mma --sum --ave < data.txt
    mma: Error! invalid option --sum
    497.530
    chessie [80] [~stahl]> mma < data.txt
    2.350  999.268 497.530
    chessie [80] [~stahl]>
Here's the expected behavior for an input file that has no data (i.e., is empty):
    chessie [80] [~stahl]> mma < empty.txt
    mma: Error - empty input, min/max/average undefined!
    chessie [80] [~stahl]>
Note that mma invoked with no options prints out all three quantities, separated by tabs, to three decimal places - nothing more, nothing less. mma invoked with duplicate options prints the requested information as many times as it is requested. mma invoked with an invalid option prints the error message as shown, to stderr, but otherwise simply ignores the invalid option.

Part 3 (20 points extra credit): Filename on command-line
Augment your mma.c file so that the mma program can alternately read from a file whose name is given on the command-line. You may assume that any string that does not begin with "--" is to be treated as an input file name. Without a filename on the command-line, mma reads from stdin, just as before.
    chessie [81] [~stahl]> mma data.txt
    2.350  999.268 497.530
    chessie [82] [~stahl]> mma < data.txt
    2.350  999.268 497.530
    chessie [83] [~stahl]> mma --max data.txt
    999.268
    chessie [84] [~stahl]> mma lkjlkjlkj
    mma: Error! File "lkjlkjlkj" not found!
    chessie [85] [~stahl]> mma --sum --ave data.txt
    Error: invalid option --sum
    497.530
    chessie [86] [~stahl]> mma --help
    mma [--min] [--max] [--ave] [--help] [filename]

Note: If you're clever, this can be done with very little modification to your original code. Some additions, but very little within the code you've already written needs to be changed.

Submission
This (mini)lab is due Friday, 26 February, prior to your class period. Use the submit script to submit the entire directory lab04. This must include the file mma.c, and the input files dat.txt and empty.txt that came from untarring the file lab04.tar.gz, and a text file called README that includes your name and alpha code, a clear statement of whether your submission is a Part 1, Part 2 or Part 3 submission, and the answers to the following questions (20 Points):
1. What information does the value "returned" by a process usually convey?



2. Why would it be a bad idea to print out 0 for the min/max/ave
   values when the input is empty?


3. Assuming the declarations:

   int k, n;
   double x, y;
   char *s;
   char *t;

   Give the type of each of the following expressions assuming

   a. fscanf(stdin,"%i %lf",&k,&x)
   
   b. s

   c. t[3]

   d. &t
 
   e. k/n

   f. x/n

   g. *s

   h. malloc(10*sizeof(double)

   i. fopen("foo.txt","w")

   j. fclose(stdout)

   k. stdout
   
4. What does it mean if strcmp(s,t) returns a negative number?
   HINT: RTFM (i.e. Read The F****** Manpage).


	      


Christopher W Brown
Last modified: Thu Feb 12 13:30:27 EST 2009