Homework #2

Assigned: January 30
Due: February 6, 3:00pm

Problem

(32 points) Implement the UNIX wc command in C.

Requirements

  1. Your program must be written in C (not C++) and compile without errors or warnings using gcc on our system.
  2. Your version of wc must behave exactly like the wc command installed on our system in all aspects with the following exception. You must only implement the -l, -w, and -c options. It is your responsibility to mine the behavior of wc on our system and replicate it in your program (see the wc manpage and experiment with the command thoroughly). However, the following is some guidance to get you started in thinking about the behavior of wc:

    1. All options must precede all input filenames.
    2. If no input files are given as command-line arguments, wc defaults to standard input.
    3. wc always writes to standard output.
    4. Options can be given individually and in any order (e.g., -c -l) or in one stoke (e.g., -lc).
    5. The order in which the options are supplied has no effect on the order in which the counters are displayed. The number of lines are always printed first, followed by the number of words and characters.
    6. If no options are given, wc prints the number of lines, words, and characters.
    7. If an invalid option or filename is given, your program must print the same error message wc would print to standard error in that particular situation and halt with the same non-zero exit status.
    8. Use field-width and precision in your formatted output.

Hints

If designed properly, the program required to solve this homework problem should occupy no more than 150 lines of code. Furthermore, the interested student is encouraged to investigate the getopt function (see man -s 3 getopt) to simplify parsing command-line options, and to factor command-line arguments from file arguments. The use of getopt is not required. If you are still getting acclimated to UNIX and C, you should avoid the use getopt, and parse the command-line options manually.

Sample test data

There is a transcript of a UNIX session here which illustrates the execution the system's wc command on several test cases. The input files used in the examples actually live on our UNIX system (see the particular computer on which they were run at the top of the file) and you are encouraged to test your program with them for purposes of comparison. These test cases are not exhaustive.

How to submit

Note: All directory and filenames below are case-sensitive. You must use the directory and filenames exactly as shown below (i.e., all lower case).

Prepare your source code file as /home/<logname>/homeworks/hw2/wc.c, where <logname> is your login name (e.g., FA_12_CPS346_03). Only your source file wc.c will be electronically collected from this location in your UNIX account on the deadline.

Failure to follow these submission requirements will result in a 10% penalty.


Return Home