error - but which program??

chris at umcp-cs.UUCP chris at umcp-cs.UUCP
Sat Jun 9 10:01:08 AEST 1984


*	From: jpl at allegra.UUCP

	... don't you just love it when you run a pipeline of commands
	and one of them prints:
	    malloc failed.
	and then dies without identifying itself?

Here's where I'd like to see a small addition to the C library...:

    NAME
	error - print an error message and optionally exit

    SYNOPSIS
	error(quit, syserr, fmt, arg, ...)
	int quit;
	int syserr;
	char *fmt;

    DESCRIPTION

	Prints (to stderr) the name of the program, followed by a colon
	and a space, and then performs a printf() to stderr using the
	given format and arguments.  Then, if syserr is nonzero, prints
	the system error message associated with that error index (that
	is, prints what ``perror'' would).  Finally, it prints a newline
	and calls fflush(stderr).

	If quit is nonzero, the program is terminated (via ``exit(quit);'').
	Otherwise, control is returned to the caller.

	The program name is squirreled away in the external ``ProgName''.

    SEE ALSO
	intro(2), exit(2), fprintf(3S), fflush(3S), perror(3)

    EXAMPLE
	.
	.
	.
	char *fname;
	FILE *f;
	extern int errno;
	.
	.
	.
	f = fopen(fname, "r");
	if (f == NULL)
		error(1, errno, "can't read %s", fname);
	if (isbad(f)) {
		error(0, 0, "file %s is not in the correct format; rebuilt",
			fname);
		rebuild_it(f, fname);
	}

    BUGS
       The system error message can only appear at the end.  It would
       be nice to have a printf() format specifier to produce the system
       error message from the error index number.
------------------------------------------------------------------------
The function is obviously implementable on any machine that has fprintf().
In fact, I like it so much that I've taken to using it even though it's
not in the C Library.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP:	{seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.lang.c mailing list