aboutsummaryrefslogtreecommitdiff
path: root/src/jtutil/error_exit.c
blob: 26acdf757d10fe723f6d16ef40c904a69ceb6ffb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* error_exit -- print an error message and exit */
/* $Id$ */
/*
 * error_exit -- print an error message and exit
 */

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

#include "stdc.h"

/*****************************************************************************/

/*
 * This function prints an error message (formatted via vfprintf(3S)),
 * then exits.  It does *not* return to its caller.  Normally the exit
 * is done via exit(2), but optionally it may be done via abort(2) to
 * produce a core dump.  Despite not actually returning, this function
 * is declared as returning an  int , so it may be easily used in
 * conditional expressions like
 *      foo = bar_ok ? baz(bar) : error_exit(...);
 *
 * Usage:
 *	error_exit(exit_code, message, args...)
 *
 * ***** THIS VERSION IS FOR ANSI/ISO C *****
 *
 * Arguments:
 * exit_code = (in) Exit code for exit(2), or PANIC_EXIT (defined in
 *		    <jt/stdc.h>) to abort(2) instead.
 * format = (in) vprintf(3S) format string for error message to be printed.
 * args... = (in) Any additional arguments are (presumably) used in formatting
 *		  the error message string.
 */
/*VARARGS*/
int error_exit(const int exit_code, const char *const format, ...)
{
va_list ap;

va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);

if (exit_code == PANIC_EXIT)
   then abort();						/*NOTREACHED*/
   else exit(exit_code);					/*NOTREACHED*/
}