My error-handling library and ANSI prototypes (Part 0/2)

Bennett Todd bet at dukeac.UUCP
Sat Feb 4 11:46:58 AEST 1989


I am about to post, in two shell archives, my error-handling library of
wrappers around system calls and library routines (sections 2 and 3 UPM). This
package includes (wrapped in the second shell archive) a header file which in
turn includes /usr/include/*, more-or-less, and then has ANSI function
prototypes for most all of them. I say most, because there are a couple of
exceptions and strangenesses. Frst I created the thing using the 4.3BSD
manual, including prototypes for everything (except I didn't get around to
doing the big packages like curses, dbm, lib3248, and so forth; you can grep
for TBD to see what I deliberately skipped). Then I tried to use it on the
Sun, and made a few changes under "#ifdef sun" to make it useable. I have
since added a few routines not in 4.3BSD's man pages, but I have made no
attempt to cover all of Sun's additional routines.

Here's an inane example of using the library:

#include <bent.h>
/*
 * cat [file ...]
 *
 * Copy file(s) (stdin default) to stdout.
 * Gratuitously balks if it thinks it sees a "-" option.
 * Cat shouldn't take any options.
 */

/* This here is all that is required to provide the "syntax()" subroutine */
char syntax_args[] = "[file ...]";

#define BLEN 65536

static void cat(FILE *);

int main(int argc, char **argv) {
	/* "progname" is used by the library routines for error messages */
	progname = argv[0];
	argc--, argv++;

	if (argc > 0 && **argv == '-')
		syntax();

	if (argc > 0) {
		while (argc-- > 0)
			cat(efopen(*argv++, "r"));
	} else {
		cat(stdin);
	}
}

static void cat(FILE *fp) {
	static char *buffer = NULL;
	int n;

	if (buffer == NULL)
		buffer = emalloc(BLEN);

	while (n = fread(buffer, 1, BLEN, fp))
		efwrite(buffer, 1, n, stdout);

	efclose(fp);
	return;
}

That is how I am writing programs these days; it doesn't take me any extra
time, they can (once they are debugged) be compiled using gcc with all
warnings turned on and not generate any, and run-time error returns from the
system are systematically checked and uniformly reported.

I like it. I am posting it because I've gotten many requests for it; I am
posting it to this group because mail between here and the moderator of
comp.sources.misc seems to be hosed (I've tried mailing this to him a couple
of times; nary a bounce or confirmation or whatever. I am sure it is my
fault).

-Bennett
bet at orion.mc.duke.edu



More information about the Comp.lang.c mailing list