forked from ulfalizer/nesalizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.cpp
More file actions
47 lines (41 loc) · 1.27 KB
/
error.cpp
File metadata and controls
47 lines (41 loc) · 1.27 KB
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
#include "common.h"
static void fail_helper(bool include_errno, char const *format, va_list args)
__attribute((format(printf, 2, 0), noreturn));
static void fail_helper(bool include_errno, char const *format, va_list args) {
fprintf(stderr, "%s: ", program_name);
vfprintf(stderr, format, args);
if (include_errno) {
char const*const err_str = strerror(errno);
fprintf(stderr, ": %s (errno = %d)", err_str ? err_str : "unknown error", errno);
}
putc('\n', stderr);
exit(EXIT_FAILURE);
}
void fail(char const *format, ...) {
va_list args;
va_start(args, format);
fail_helper(false, format, args);
}
void fail_if(bool condition, char const *format, ...) {
va_list args;
if (condition) {
va_start(args, format);
fail_helper(false, format, args);
}
}
void errno_fail_if(bool condition, char const *format, ...) {
va_list args;
if (condition) {
va_start(args, format);
fail_helper(true, format, args);
}
}
// Like errno_fail_if() but with a specified errno value
void errno_val_fail_if(bool condition, int errno_val, char const *format, ...) {
va_list args;
if (condition) {
va_start(args, format);
errno = errno_val;
fail_helper(true, format, args);
}
}