#include <useful.h>
#include <funclist.h>

VERSIONID("$Header: syserr.c,v 2.0 89/12/24 00:56:31 eric Exp $");

/*
**  SYSERR -- indicate a system error
**
**	Parameters:
**		m -- the printf() message to print.
**		a, b, c, d, e -- parameters to the message
**
**	Returns:
**		never
**
**	Side Effects:
**		Terminates the process if no acceptable recover takes place.
**
**	Globals:
**		SyserrFuncs -- a list of functions to call.
**
**	Author:
**		Eric Allman
**		University of California, Berkeley
*/

VOID
syserr(m, a, b, c, d, e)
	char *m;
{
	static BOOL exiting = FALSE;

	/* print the error message */
	_error_message(m, a, b, c, d, e);

	/* if we recursively syserr during exit, drop out now! */
	if (exiting)
		_exit(1);

	/* try a clean exit */
	exiting = TRUE;
	exit(1);
	/*NOTREACHED*/
}
/*
**  USRERR -- print a user error
**
**	Parameters:
**		m -- the printf() message to print.
**		a, b, c, d, e -- parameters to the message
**
**	Returns:
**		none.
**
**	Side Effects:
**		clears the global error number.
*/

VOID
usrerr(m, a, b, c, d, e)
	char *m;
{
	extern int errno;

	/* print the error message */
	_error_message(m, a, b, c, d, e);

	/* give us a clean slate */
	errno = 0;
}
/*
**  _ERROR_MESSAGE -- generic message printing routine.
**
**	Includes the program name with the message, as well as the
**	various possible error codes.
**
**	Parameters:
**		m -- the printf() message to print.
**		a, b, c, d, e -- parameters to the message
**
**	Returns:
**		none.
**
**	Side Effects:
**		none.
**
**	Globals:
**		ProgName -- the name of the program.
*/

STATIC VOID
_error_message(m, a, b, c, d, e)
	char *m;
{
	extern char *ProgName;
	int saveerr;
	extern int errno;

	saveerr = errno;
	if (ProgName != CHARNULL)
		fprintf(stderr, "%s: ", ProgName);
	fprintf(stderr, m, a, b, c, d, e);
	fprintf(stderr, "\n");
	if (saveerr != 0)
	{
		errno = saveerr;
		perror("System error");
	}
}
