Due by 11:59.59pm Tuesday, April 11 2017
Note: You may work with a partner on this project. Let me know who you are working with and your Github account names, and I'll set up a shared project for this assignment. As there are a number of components to this assignment, I'd encourage you to get started before the last minute.
As with the previous assignment, you will be creating a copy of a commonly used Unix tool. In this case, you will be working on a tool that allows you to sort text in various ways.
You'll also learn about the C99 replacement for atoi() and get some additional experience working with strings.
As with the previous assignment, I'd like you to read through the project specification and then formulate an estimate of the length of time that you expect it will take to complete the project. Record your estimate in the README before you begin coding.
The tool you'll be creating is sort, modeled after the Unix tool of the same name. This program reads in lines of text and then sorts them based on specified criteria. By default, it does so using the ordering of strcmp().
INPUT: ALICE bob BOB CAROL alice carol
OUTPUT: ALICE BOB CAROL alice bob carol
Notice how all the capital letters come before the lower case ones? That is not a mistake. The numeric value of 'A' is 32 less than the value of 'a' so it is natural for them to come first. (See notes below if odd behavior occurs.)
However, sometimes you don't want this to take place. Therefore you should support the -f flag which folds lowercase letters into uppercase ones (i.e., case insensitive comparisons).
OUTPUT: ALICE alice BOB bob CAROL carol
Ordering of otherwise identical upper vs. lower case lines is up to the sort, not something you need to handle.
Sometimes, you want to sort lines based on a leading number. To do that you'll use the -n flag. Note that this should ignore leading whitespace, treat the first thing as a number, then sort based on the rest of the line as before if multiple lines have the same leading number.
INPUT: 0 Alice 99 Bob 20 Carol 172 Eve 76 Mallory 9 Trent 59 Plod 14 Steve
OUTPUT: 0 Alice 9 Trent 14 Steve 20 Carol 59 Plod 76 Mallory 99 Bob 172 Eve
I'd like you to do this by implementing long mystrtol(char *start, char **rest) based on the strtol() function added in to C99 to replace atoi().
This function should take a pointer to the start of a string, skip leading whitespace, and attempt to convert the next characters into a long integer. If it isn't a number, you should return 0.
The second parameter is where you can pass in the address to a character pointer. If this value is not NULL, you should store the address of the first character after the number you interpreted. If no number was interpreted, you should just set it to the start of the line, whitespace included.
NOTE: The real version of strtol() takes a third parameter that lets you specify the base of the number being interpreted, but we aren't implementing that.
/* Example of mystrtol */ char line = " -56 Cards in a negative deck"; long value; char *rest; value = mystrtol(line, &rest); // After the call, the following should be true assert(value == -56); assert(rest == &line);
Some guidelines you should follow when working on your solution:
In addition to the flags listed above, I'd like you to implement a -h and -? flag that prints out a brief usage message and then exits the program with a non-zero value. You should do the same behavior if an unknown flag is passed to your program.
You should have your program's main function return 0 upon successful completion of the assigned task.
As with HW 5, I want you to also create a man page for your new tool. This one should be called sort.1.
Create a file called README that contains
Now you should make clean to get rid of your executables and handin your folder containing your source files, Makefile, and README.
% cd ~/cs241 % handin -c 241 -a 6 hw6 % lshand
You sometimes want things in descending order instead of ascending order. To do this, you will support the -r flag to reverse the sort, regardless of the other options specified. So you can do reverse numeric, folded, or normal sorts.
Here is what I am looking for in this assignment: