aboutsummaryrefslogtreecommitdiff
path: root/src/tools/entab/halt.c
blob: dfc5936fadc8ecdbf066ccfe61535fc3a881b836 (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
49
50
51
52
53
54
55
56
57
58
/*
**
**		halt.c
**
** src/tools/entab/halt.c
**
**		This is used to print out error messages and exit
*/

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


/*-------------------------------------------------------------------------
**
**		halt - print error message, and call clean up routine or exit
**
**------------------------------------------------------------------------*/

/*VARARGS*/
void
halt(const char *format,...)
{
	va_list		arg_ptr;
	const char *pstr;
	void		(*sig_func) ();

	va_start(arg_ptr, format);
	if (strncmp(format, "PERROR", 6) != 0)
		vfprintf(stderr, format, arg_ptr);
	else
	{
		for (pstr = format + 6; *pstr == ' ' || *pstr == ':'; pstr++)
			;
		vfprintf(stderr, pstr, arg_ptr);
		perror("");
	}
	va_end(arg_ptr);
	fflush(stderr);

	/* call one clean up function if defined */
	if ((sig_func = signal(SIGTERM, SIG_DFL)) != SIG_DFL &&
		sig_func != SIG_IGN)
		(*sig_func) (0);
	else if ((sig_func = signal(SIGHUP, SIG_DFL)) != SIG_DFL &&
			 sig_func != SIG_IGN)
		(*sig_func) (0);
	else if ((sig_func = signal(SIGINT, SIG_DFL)) != SIG_DFL &&
			 sig_func != SIG_IGN)
		(*sig_func) (0);
	else if ((sig_func = signal(SIGQUIT, SIG_DFL)) != SIG_DFL &&
			 sig_func != SIG_IGN)
		(*sig_func) (0);
	exit(1);
}