Sum, Revisited

October 21, 2011

We looked at the Unix SysV sum command in a previous exercise. At the time, my intention was to do the BSD version of the sum command immediately, in the next exercise, but for some reason I failed to do so. That oversight is corrected in today’s exercise.

The original SysV sum command simply calculated the sum of all the bytes in the file, modulo 216. Thus, it failed to distinguish two files in which the order of the bytes was changed, null bytes were added, or the bytes were manipulated so that additions to one byte were subtracted from another byte. Although the BSD algorithm isn’t perfect, it identifies all those differences by summing 2-byte words, rotating the accumulating sum one bit to the right after each step.

Today’s exercise is to implement the standard Unix sum command that calculates either of these checksums:

NAME

sum -- checksum and count the blocks in a file

SYNOPSIS

sum [-r | -s] [file ...]

DESCRIPTION

Print checksum and block counts for each file. The BSD algorithm is used unless the System V algorithm is specified.

-r -- Use BSD sum algorithm with 1024-bit blocks.

-s -- Use System V sum algorithm with 512-bit blocks.

When file is - or is not given, read standard input.

Your task is to write the Unix V7 sum command as specified above. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.