Source Code

Here is the source code to a few of the things I've written. Most of these are utilities, useful in the context of Unix-style ``toolbox'' programming. Several of them have become indispensible in my own work; I use them daily.

A few of these packages are also candidate answers to a frequently-asked but difficult-to-answer question: ``Where is some C code I can study and learn from?'' The packages I can especially recommend for this purpose are marked with a red asterisk, like this: *.

A couple of the packages exist in two versions: a ``simple'' version which is presentable as a single .c file, and a more full-featured one which ends up requiring a whole set of source files and a Makefile. Digital signatures for all packages are available, though not (yet) explicitly linked to from here -- see the ftp directory for details.

Most of these source files are Copyright © by me, but I am publishing them with few restrictions: you're free to use them for just about any purpose, so long as you leave my name attached, mark any changes you make as yours, and don't otherwise try to pass these works off as your own. (And I've placed several of these source files explicitly in the public domain.)

* count - generate integer sequences
* line - extract lines from a file
column - extract columns from a file
dateexpr - perform date and time calculations
bsearch - find line(s) in sorted file
dbgrep - extract and process records from simple text databases
med - (stream) math editor
sleep - delay for (or until) a time
* randline - deliver random line(s)
shuffle - rearrange lines in random order
extract - extract byte ranges from file or stream
stat - print file's stat(2) information
filter - run a filter over several files, in-place
dbm tools - manipulate Unix dbm (also gdbm) files
* strftime - format time (struct tm) information
stdio - a reimplementation of the C <stdio.h> library

count

A tool for generating sequences of numbers, useful among other things for writing shell loops:

	for I in `count 10`; do ...; done

documentation   simple source code *   source code (.tar.gz)   source code (.zip)   precompiled binaries

line

A tool for extracting selected lines (by line number) from text files.

documentation   simple source code *   source code (.tar.gz)   source code (.zip)   precompiled binaries

column

A tool for extracting selected columns from text files (à la the standard ``cut'' program).

documentation   source code (.tar.gz)   source code (.zip)   precompiled binaries

dateexpr

Date and time calculations are notoriously awkward, and there's no good way of performing them from within a shell script. (An acquaintance once wrote a script that invoked cal(1) and parsed its output in order to do date calculations.) dateexpr is a general-purpose command, along the lines of expr(1), that lets you add and subtract (and perform several more operations on) dates and times: computing the difference between two times, adding N days to a date, comparing dates and times, etc.

documentation   source code (.tar.gz)   precompiled binaries

bsearch

When you have sorted data, an efficient way of finding an item in it (or determining that an item is not there) is binary search. This program does so for sorted text files. It is a companion to the standard Unix sort(1) utility, accepting most of the same options. (The idea is that you can give bsearch the same options you gave sort when creating the sorted file to be searched.) This program is, in some sense, a descendant of the bsd Unix ``look'' utility, and in fact can be invoked as a compatible replacement.

documentation   source code (.tar.gz)   source code (.zip)   precompiled binaries

dbgrep

Stated simply, a tool to ``extract and process records from simple text databases''. Stated another way, a tool to use when the standard Unix model of ``everything's a text file, and a text file consists of lines of text'' isn't quite enough. Specifically, when you have a text file where lines are keyword/value pairs clumped together into ``records'' which should be treated together as a unit, like this:

	name: cat
	genus: felis
	species: domesticus
	young: kitten

	name: dog
	genus: canis
	species: familiaris
	young: puppy

	name: horse
	genus: equus
	species: caballus
	young: foal
how can you manipulate the file, without writing programs (or awk or perl scripts)? dbgrep is a general-purpose tool for manipulating such files.

documentation   source code (.tar.gz)   source code (.zip)   precompiled binaries

med

The ``math editor'', a filter for processing columns of numbers and performing arithmetic manipulations on them.

documentation   source code (.tar.gz)   source code (.zip)   precompiled binaries

sleep

A reimplementation of the standard Unix `sleep' utility, with a number of creeping feat... er, ``extensions''. (Among other things: an optional HH:MM:SS format, and subsecond resolution.)

documentation   source code   precompiled binaries

randline

A filter for selecting random lines from text files, using the classic one-pass algorithm described in Knuth.

documentation   simple source code *   source code (.tar.gz)   precompiled binaries

shuffle

A filter for shuffling lines in a text file into random order, basically the opposite of sort(1).

(randline and shuffle are related, in a way: for example, randline -n could be implemented as shuffle | head -n.)

documentation   source code (.tar.gz)   precompiled binaries

extract

A tool for extracting selected byte ranges from files or streams.
(Part of the same package as
line, which is why it shares the same source files.)

documentation   source code (.tar.gz)   source code (.zip)   precompiled binaries

stat

A simple tool for printing a file's ``stat'' (or inode) information; a ``chapter 1 interface to a chapter 2 system call''.

documentation   source code   precompiled binaries

filter

A shell script for running a filter program on a number of input files and replacing each file with the filtered output.

documentation   program (sh script)   package (.tar.gz)

dbm tools

A set of command line tools for manipulating Unix dbm files.

documentation   source code (.tar.gz)   precompiled binaries

strftime

An implementation of the ANSI C time-formatting function (along with a command-line callable version).

source code *

stdio

A reimplementation of C's stdio library.

source code (.tar.gz)