Computer Science 381
Programming Unix in C
Winter Immersion 2014, The College of Saint Rose
In this, your final lab assignment, you will learn about some of the more advanced usage of C's pointers, and a few other items of importance that did not make it into previous labs.
Assorted Unix Fun
There are a number of Unix commands or options to commands that are well worth knowing about that have not come up so far. Here are a few to experiment with:
cal units yes history strings file cd - pushd popd tail -f du fg, bg, kill ps ps -auxw top tee xargs
Handling Command-Line Parameters
We have seen that you can retrieve the command line parameters to a
program through the argc and argv parameters to your
program's main function. You might have noticed that many of the
Unix commands you've learned about this semester make heavy use of
command-line parameters. These often are switches that start with
-
or --
. For example, see the man page for ls.
You can imagine that simply parsing the command line parameters to
decide what the command is supposed to do can be a complex task.
The C standard library provides a mechanism to parse these command-line parameters more easily and efficiently: getopt. See the description in section 3 of the manual: getopt(3).
See the following example for some ways to make use of this utility.
See Example:
/home/cs381/examples/getopt-ex
Function Pointers/Callbacks
Section 5.11 of K&R shows you a way you can write a sort function to sort data in multiple ways by specifying a function pointer as one of the parameters to the sort function. In this case, this would be a comparator function that knows how to compare two values during the swap procedure. This idea is formalized in Java with the Compatator interface, but as usual, in C, we can achieve similar functionality but without the formal language or API support.
Make sure you understand how this works, then look at the qsort function provided by the C standard library (man qsort). This works similarly to the one in K&R, but works on an arbitrary array of any data type.
Some examples of qsort in action:
See Example:
/home/cs381/examples/qsort_examples
For another way to use function pointers in a way somewhat like an iterator, see the function sll_visit_all and its usage in the previous example:
See Example:
/home/cs381/examples/sll
Submission
Please submit all required files as email attachments to terescoj AT strose.edu. You are recommended to do so by Thursday, January 16, 2014. Be sure to check that you have used the correct file names and that your submission matches all of the submission guidelines listed on the course home page.
Grading
Grading Breakdown | |
Lab question | 10 points |
getopt enhancement | 10 points |
qsort enhancement | 15 points |
find_max enhancement | 5 points |