-
Notifications
You must be signed in to change notification settings - Fork 0
/
cerr.h
102 lines (93 loc) · 3.06 KB
/
cerr.h
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
/**
* @file cerr.h
* @author Jens Munk Hansen <jmh@debian9laptop.parknet.dk>
* @date Thu Mar 8 21:57:32 2018
*
* @brief
*
* Copyright 2017 Jens Munk Hansen
*/
/*
* This file is part of SOFUS.
*
* SOFUS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SOFUS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SOFUS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <sps/stdio.h>
#include <sps/cenv.h>
#include <string.h> // strerror
#include <errno.h>
/**
* Macro for calling functions.
*
* @param fun
* @param arg
*
*/
#define CallErr(fun, arg) { \
if ((fun arg)<0) \
FailErr(#fun) \
}
/**
* Macro for calling functions. Functions registered using atexit will
* be called before the process exists
*
* @param fun
* @param arg
*
* @return
*/
#define CallErrExit(fun, arg) { \
if ((fun arg)<0) { \
FailErr(#fun); \
exit(EXIT_FAILURE); \
} \
}
/**
* Macro for calling functions. If function fails (retval < 0), the
* process will be terminated without calling functions registered
* using atexit.
*
* @param fun
* @param arg
*
* @return
*/
#define CallErr_Exit(fun, arg) { \
if ((fun arg)<0) { \
FailErr(#fun); \
_exit(EXIT_FAILURE); \
} \
}
#define FailErr(msg) { \
(void)fprintf(stderr, "%s, %s(%d)\n", \
msg, strerror(errno), errno); \
(void)fflush(stderr);}
#define CallErrReturn(fun, arg, ret) { \
if ((fun arg)<0) { \
FailErr(#fun); \
return ret; \
} \
}
#define CallZeroErrReturn(fun, arg, ret) { \
if ((fun arg) == 0) { \
FailErr(#fun); \
return ret; \
} \
}
/* Local variables: */
/* indent-tabs-mode: nil */
/* tab-width: 2 */
/* c-basic-offset: 2 */
/* End: */