Homework #1

Assigned: August 30
Due: September 8, 4:30pm


(71 points) Implement a stripped down version of the UNIX file comparison program diff in C.


  1. Your program must be written in C (not C++) and compile without errors or warnings using gcc on our system.
  2. The two input files are given on the command line using file I/O. E.g.,
    $ ./a.out file1 file2
  3. Two files are identical if they match exactly character by character.
  4. If the two input files are identical, print nothing to standard output.
  5. If the two input files are different, print the line numbers (the first line of each file is line 1) on which they differ one per line. E.g.,
    $ ./a.out file1 file2
  6. A file name of - stands for text read from the standard input.
  7. As a special case, diff - - compares a copy of standard input to itself.
  8. Normal program must only be written to standard output.
  9. Support the following command-line options:
    1. -l: ignore leading whitespace in the comparison, where whitespace is any contiguous series of tabs or spaces.
    2. -t: ignore trailing whitespace in the comparison.
    3. -m: ignore intermediary whitespace in the comparison (i.e., whitespace neither at the beginning or the end of each line).
    4. -a: ignore all whitespace in the comparison.

  10. All options must precede both input filenames.
  11. Options can be given individually and in any order. For instance, -l -t, or in one stoke (e.g., -tl).
  12. If no options are given, the comparison is exact.
  13. If an invalid option or filename is given, your program must print the same error message diff would print to standard error in that particular situation and halt with the same non-zero exit status.
  14. If any other option, valid or otherwise, is given with the -a option, your program must print the following error message to standard error and halt with exit status 9:
    $ ./a.out -t -a file1 file2
    Option -a cannot be combined with any other options.
  15. Prepare your source code file as /home/<logname>/homeworks/hw1/diff.c.


If designed properly, the program required to solve this homework problem should occupy no more than 200 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.

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/hw1/diff.c, where <logname> is your login name (e.g., cps444-n1.01). Only your source file diff.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.


Ninety percent of your score will come from correctness and 10% of your score will come from following our programming style guide. Applicable submission penalties will then be applied.

Return Home