Assignment 5. C programming and debugging

Useful pointers

Laboratory: Debugging a C program

As usual, keep a log in the file lab5.txt of what you do in the lab so that you can reproduce the results later. This should not merely be a transcript of what you typed: it should be more like a true lab notebook, in which you briefly note down what you did and what happened.

You're helping to maintain an old stable version of coreutils, but that version has a bug in its implementation of the ls program. (Actually, it has two bad bugs, but for now we'll just look at the first one.)

The bug is that ls -t mishandles files whose time stamps are very far in the past. It seems to act as if they are in the future. For example:

$ touch -d '1918-11-11 11:00 GMT' wwi-armistice
$ touch now
$ sleep 1
$ touch now1
$ ls -lt wwi-armistice now now1
-rw-r--r-- 1 eggert eggert 0 Nov 11  1918 wwi-armistice
-rw-r--r-- 1 eggert eggert 0 Feb  5 15:57 now1
-rw-r--r-- 1 eggert eggert 0 Feb  5 15:57 now

Use a debugger to figure out what went wrong and to fix the corresponding source file.

Construct a patch file lab5.patch containing your fixes, in the form of a ChangeLog entry followed by a diff -u patch.

Homework: Binary sort

Write a program binsort that takes a single argument N (where N is a positive decimal integer), reads N-byte records from standard input, sorts them, and outputs the result to standard output. If standard input ends in a partial record that contains fewer than N bytes, binsort should treat it as if it were padded with trailing null ('\0') bytes.

Use lexicographic byte comparison to compare each record, in the style of the memcmp function. Use <stdio.h> functions to do I/O. Use malloc, realloc and free to allocate enough storage to hold all the input, and use qsort to sort the data. Accept any positive decimal integer string N that strtoul would. If the program encounters an error of any kind (including input, output or memory allocation failures, missing or extra arguments, a N that is not a positive integer or is too large to be represented), it should report the error to stderr and exit with status 1; otherwise, the program should succeed and exit with status 0. The program need not report stderr output errors.

For example, the command:

printf 'D\x00C\x00\x00B\x00A' | ./binsort 2 | od -c

should output:

0000000  \0   A  \0   B   C  \0   D  \0
0000010

Also, assuming your source code has no lines longer than 100 bytes, the command:

awk '{printf "%-127s\n", $0}' binsort.c | ./binsort 128 | sed 's/ *$//'

should output something that is nearly equivalent to the output of the command:

LC_ALL='C' sort binsort.c

Submit

Submit the following files.

All files should be ASCII text files, with no carriage returns, and with no more than 200 columns per line. The C source file should contain no more than 132 columns per line. The shell commands

expand lab5.txt lab5.patch | awk '/\r/ || 200 < length'
expand binsort.c | awk '/\r/ || 132 < length'

should output nothing.


© 2005, 2007, 2008, 2009 Paul Eggert. See copying rules.
$Id: assign5.html,v 1.9 2009/01/30 00:31:17 eggert Exp $