Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10 5/3/83; site umcp-cs.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!floyd!cmcl2!seismo!rlgvax!cvl!umcp-cs!chris
From: chris@umcp-cs.UUCP
Newsgroups: net.unix-wizards,net.lang.c
Subject: Re: error - but which program??
Message-ID: <7429@umcp-cs.UUCP>
Date: Fri, 8-Jun-84 20:01:08 EDT
Article-I.D.: umcp-cs.7429
Posted: Fri Jun  8 20:01:08 1984
Date-Received: Sun, 10-Jun-84 07:02:51 EDT
References: <311@zeus.UUCP> <2515@allegra.UUCP>
Organization: Univ. of Maryland, Computer Science Dept.
Lines: 66

*	From: jpl@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@umcp-cs		ARPA:	chris@maryland