Homework #2Assigned: January 21
Due: January 28, 3:00pm, in class
- (1 point) Problem 1 is accessed and submitted
through GOAL; see
Gradiance
student guide for more information.
- (31 points) Implement the UNIX wc
command in C.
Requirements
- Your program must be written in C (not C++)
and compile without errors or warnings using
gcc on our system.
- 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:
- All options must precede all input
filenames.
- If no input files are given as
command-line arguments, wc
defaults to standard input.
- wc always writes to standard
output.
- Options can be given individually and
in any order, e.g., -c -l, or in
one stoke, e.g., -lc.
- 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.
- If no options are given, wc
prints the number of lines, words, and
characters.
- 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.
- 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 dataThere 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 submitNote: 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., cps346-01.01. Your source
file wc.c will be electronically
collected from this location in your UNIX
account on the deadline.
- Convert your source code file into a
pretty-printed pdf file by executing the
following command lines on our system from the
directory in your UNIX account containing your
source file wc.c.
a2ps wc.c -o wc.ps
ps2pdf wc.ps
or simply
which will cause wc.c to be
printed to the printer in the AN 131 lab.
- Submit a printout of wc.pdf in
class at 3:00pm.
Failure to follow these submission
requirements will result in a 10% penalty.
|