]> arthur.barton.de Git - netatalk.git/blob - include/atalk/errcheck.h
Error checking macros
[netatalk.git] / include / atalk / errcheck.h
1 /*
2    Copyright (c) 2010 Frank Lahm <franklahm@gmail.com>
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8  
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13  */
14
15 #ifndef ERRCHECK_H
16 #define ERRCHECK_H
17
18 #define EC_INIT int ret = 0
19 #define EC_CLEANUP cleanup
20 #define EC_EXIT return ret
21
22 /* 
23  * We have these macros:
24  * EC_ZERO_PRINT
25  * EC_ZERO
26  * EC_NEG1_PRINT
27  * EC_NEG1
28  * EC_NULL_PRINT
29  * EC_NULL
30  */
31
32 /* check for return val 0 which is ok, every other is an error, prints errno */
33 #define EC_ZERO_PRINT(a) \
34     do { \
35         if ((a) != 0) { \
36             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
37             ret = -1; \
38             goto cleanup; \
39         } \
40     } while (0)
41
42 /* check for return val 0 which is ok, every other is an error */
43 #define EC_ZERO(a) \
44     do { \
45         if ((a) != 0) { \
46             ret = -1; \
47             goto cleanup; \
48         } \
49     } while (0)
50
51 /* check for return val 0 which is ok, every other is an error, prints errno */
52 #define EC_NEG1_PRINT(a) \
53     do { \
54         if ((a) == -1) { \
55             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
56             ret = -1; \
57             goto cleanup; \
58         } \
59     } while (0)
60
61 /* check for return val 0 which is ok, every other is an error */
62 #define EC_NEG1(a) \
63     do { \
64         if ((a) == -1) { \
65             ret = -1; \
66             goto cleanup; \
67         } \
68     } while (0)
69
70 /* check for return val != NULL, prints errno */
71 #define EC_NULL_PRINT(a) \
72     do { \
73         if ((a) == NULL) { \
74             LOG(log_error, logtype_default, "%s failed: %s" #a, strerror(errno)); \
75             ret = -1; \
76             goto cleanup; \
77         } \
78     } while (0)
79
80 /* check for return val != NULL */
81 #define EC_NULL(a) \
82     do { \
83         if ((a) == NULL) { \
84             ret = -1; \
85             goto cleanup; \
86         } \
87     } while (0)
88
89 #endif /* ERRCHECK_H */