Homework 1: The wc program

Homework is important practice for you, but it’s not graded so you don’t have to submit it.

Overview

The first homework assignment is writing a clone of the UNIX wc command. No worries, it’s a simplified clone, it doesn’t have to support all the features of the actual wc program. But you should read the man page for wc anyway, just to see what the real program can do in addition.

The Program

Your wc program reads from standard input. Once the input it over, it prints the number of lines, words, and characters (bytes) the input had to standard output. Here are two examples for how this works:

$ ./wc
0 0 0
$ ./wc
Hi there! This is
a test for you...
2 8 36

First I started the program and then immediately hit CTRL-D to signal end-of-file. Therefore the output is 0 lines, 0 words, and 0 characters.

Then I started the program and typed the two lines (hitting RETURN twice in the process), then I typed CTRL-D. There are 2 lines, 8 words (meaning 8 stretches of characters not interrupted by white space, check the definition of “word” in the man page for wc), and a total of 36 bytes. (If you’re confused why it’s 36, remember to count the invisible newline characters as well!)

Hints

Deliverables

At minimum, you should have a file wc.c with your actual program code and a Makefile that builds the wc program with the correct compiler options. The program should compile without warnings and produce no output other than what is described above, in exactly that format.

Things to look into…

Ideally you should only have to say make to build, not make wc but we didn’t cover yet how to do it. Ideally you should be able to say make clean to remove the executable and make test to run test cases, but we didn’t cover yet how to do that either. The shell/terminal can be scripted, that’s a way to run several test cases automatically. Again, we didn’t cover that yet, but if you want to look ahead at basic shell scripting and think about how we could combine it with I/O redirection and the diff tool, that’s certainly not wasted time.