Assignment 11: More Matching
Complete assignment due: Thursday, Nov. 13th, 9:00 p.m.
Write another extension to the match program of the past two assignments. This version, match3, will add two features:
•Sorting the dictionary words. Along with this will be a feature to remove repeated words.
•Using getopt_long to process command-line arguments.
Sorting and Repeating:
When the sort option is chosen, you have two choices about how to sort the words. You can read all the dictionary words into the array of structs and then sort them. Or you can enter the words into the array in sorted order as you read them. Either choice is acceptable.
You can write an n-squared sort (bubble sort, insertion sort, and selection sort). It is okay to write a faster sort, such as quicksort; however, note that the faster sorts are also harder to get right.
If you put the words in alphabetic order while reading the dictionary file, you are performing a type of insertion sort, and it will be n-squared.
If the option to remove repeated words is present, it requires the words be sorted, even if the sort option was not selected.
getopt and getopt_long:
You have used getops in Korn shell scripts. There are three C functions that mimic most, but not all, of the same features. getopt provides the ability to have single-letter options, allows the user to specify the options in any order, and provides for options that can have (required or optional) arguments. Examples include:
-t
-a authorList.txt
This assignment will not have optional or required arguments. It is likely that future assignments will.
getopt_long extends the idea to provide support for long names. For example,
-t
--title
-a authorList.txt
--author=authorList.txt
The third C library function, getopt_long_only, provides some additional features. We will not use getopt_long_only on this assignment (see the manpage for the details about this function if you are curious).
On lectura, the three C functions are all listed on the same manpage. You can get to this manpage with either of the following1:
man -s 3 getopt
man getopt_long
For those using OS X, getopt and getopt_long are on different manpages.
getopt_long handles options in any order, recognizes invalid options, and handles required or optional arguments. It does not handle the --man or --html options that the Korn shell version handles. getopt_long also does not handle the --usage option. In the case of --usage, you are required to implement a --usage option. For match3, --usage should print:
Usage: match3 dictionary file [file ...]
OPTIONS
--usage print this information and exit
-i --insensitive look for case-insensitive matches
-s --sort sort the words in the dictionary file
-r --repeat remove repeated words in the dictionary file
Note: when -r is chosen, results will be sorted.
You will find examples of the use of getopt and getopt_long on the manpages. The manpage on lectura has a poorly written section, where “poorly” means it does not make use of defined constants. The poorly written section:
static struct option long_options[] = {
{"add", 1, 0, 0},
{"append", 0, 0, 0},
{"delete", 1, 0, 0},
{"verbose", 0, 0, 0},
{"create", 1, 0, ’c’},
{"file", 1, 0, 0},
{0, 0, 0, 0}
};
Besides the use of “magic” numbers where defined constants should be, this structure is declared in the middle of a loop. The definition should be at the start of main, or declared globally before main. Here is a better declaration:
struct option long_options[] = {
{"add", required_argument, NULL, 0},
{"append", no_argument, NULL, 0},
{"delete", required_argument, NULL, 0},
{"verbose", no_argument, NULL, 0},
{"create", required_argument, NULL, ’c’},
{"file", required_argument, NULL, 0},
{NULL, 0, NULL, 0}
};
There is an additional example available: /home/cs352/fall08/Cexamples/phaser.c is a C version of the phaser example from the Korn shell book.
Make and Functions:
As with the previous two assignments, you need to divide your code into functions. Place the functions into at least two C files and at least one header file.
Provide a Makefile that supports incremental compilation of your code. The Makefile will have a match3 target. We will type make match3 to create your match3 executable. The Makefile will also support the target: make clean. This will remove all .o files created by during compilation of your program.
Turnin: Use turnin to turn in all the files for your program. This includes the Makefile, all the C program files, and the header file(s). The command is:
turnin 352assign11 Makefile ...
Special note: If you are using your Makefile from assignment 10, be sure to change the assignment name in the Makefile turnin to be 352assign11.
See the man page for the turnin program for details on what turnin can do and how you can confirm that your file was turned in.