-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrfunction.c
102 lines (88 loc) · 2.45 KB
/
errfunction.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <tlpi.h>
#include <err_func.h>
#include <stdarg.h>
#include "enames.c.inc"
#ifdef __GNUC__
__attribute__((__noreturn__))
#endif
static void terminate(Boolean useExit3){
char *s;
s = getenv("EF_DUMPCORE");
if(s != NULL && *s != '\0')
abort();
else if(useExit3)
exit(EXIT_FAILURE);
else
_exit(EXIT_FAILURE);
}
static void outputError(Boolean useErr, int err, Boolean flushStdout, const char *format, va_list ap) {
#define BUFSIZE 500
char buf[BUFSIZE], userMsg[BUFSIZE], errText[BUFSIZE];
vsnprintf(userMsg, BUFSIZE, format, ap);
if(useErr)
snprintf(errText, BUFSIZE, " [%s %s]", (err > 0 && err <= MAX_ENAME) ? ename[err] : "?UNKNOWN?", strerror(err));
else
snprintf(errText, BUFSIZE, ":");
snprintf(buf, BUFSIZE, "ERROR%S %S\n", errText, userMsg);
if(flushStdout)
fflush(stdout);
fputs(buf, stderr);
fflush(stderr);
}
void errMsg(const char *format, ...){
va_list arglist;
int savederrno;
savederrno = errno;
va_start(arglist, format);
outputError(TRUE, errno, TRUE, format, arglist);
va_end(arglist);
errno = savederrno;
}
void errExit(const char *format, ...){
va_list arglist;
va_start(arglist, format);
outputError(TRUE, errno, TRUE, format, arglist);
va_end(arglist);
terminate(TRUE);
}
void err_exit(const char *format, ...){
va_list arglist;
va_start(arglist, format);
outputError(TRUE, errno, FALSE, format, arglist);
va_end(arglist);
terminate(FALSE);
}
void errExitEN(int errnum, const char *format, ...){
va_list arglist;
va_start(arglist, format);
outputError(TRUE, errnum, TRUE, format, arglist);
va_end(arglist);
terminate(TRUE);
}
void fatal(const char *format, ...){
va_list arglist;
va_start(arglist, format);
outputError(FALSE, 0, TRUE, format, arglist);
va_end(arglist);
terminate(TRUE);
}
void usageErr(const char *format, ...){
va_list arglist;
fflush(stdout);
fprintf(stderr, "Usage: ");
va_start(arglist, format);
vfprintf(stderr, format, arglist);
va_end(arglist);
fflush(stderr);
exit(EXIT_FAILURE);
}
void cmdLineErr(const char *format, ...){
va_list arglist;
fflush(stdout);
fprintf(stderr, "command-line usage error: ");
va_start(arglist, format);
vfprintf(stderr, format, arglist);
va_end(arglist);
fflush(stderr);
exit(EXIT_FAILURE);
}