diff options
author | Brent Cook <bcook@openbsd.org> | 2016-01-03 19:05:05 -0600 |
---|---|---|
committer | Brent Cook <bcook@openbsd.org> | 2016-01-03 21:20:03 -0600 |
commit | 07e541cc2e188bfed9f3284471cfcd02e09f6983 (patch) | |
tree | 967c39d65abb596f63ca954d1c18aaad5affe987 /include/compat | |
parent | cf86bf85812c14827be31b95a89581e243a1165f (diff) | |
download | portable-07e541cc2e188bfed9f3284471cfcd02e09f6983.tar.gz portable-07e541cc2e188bfed9f3284471cfcd02e09f6983.tar.bz2 portable-07e541cc2e188bfed9f3284471cfcd02e09f6983.zip |
replace err.h macros with inline functions
Passing NULL for the format is just easier with a function.
Diffstat (limited to 'include/compat')
-rw-r--r-- | include/compat/err.h | 62 |
1 files changed, 54 insertions, 8 deletions
diff --git a/include/compat/err.h b/include/compat/err.h index eaf7ccc..65a760f 100644 --- a/include/compat/err.h +++ b/include/compat/err.h | |||
@@ -13,20 +13,66 @@ | |||
13 | #define LIBCRYPTOCOMPAT_ERR_H | 13 | #define LIBCRYPTOCOMPAT_ERR_H |
14 | 14 | ||
15 | #include <errno.h> | 15 | #include <errno.h> |
16 | #include <stdarg.h> | ||
17 | #include <stdlib.h> | ||
16 | #include <stdio.h> | 18 | #include <stdio.h> |
17 | #include <string.h> | 19 | #include <string.h> |
18 | 20 | ||
19 | #define err(exitcode, format, ...) \ | 21 | static inline void |
20 | errx(exitcode, format ": %s", ## __VA_ARGS__, strerror(errno)) | 22 | err(int eval, const char *fmt, ...) |
23 | { | ||
24 | int sverrno = errno; | ||
25 | va_list ap; | ||
21 | 26 | ||
22 | #define errx(exitcode, format, ...) \ | 27 | va_start(ap, fmt); |
23 | do { warnx(format, ## __VA_ARGS__); exit(exitcode); } while (0) | 28 | if (fmt != NULL) { |
29 | vfprintf(stderr, fmt, ap); | ||
30 | fprintf(stderr, ": "); | ||
31 | } | ||
32 | fprintf(stderr, "%s\n", strerror(sverrno)); | ||
33 | exit(eval); | ||
34 | va_end(ap); | ||
35 | } | ||
24 | 36 | ||
25 | #define warn(format, ...) \ | 37 | static inline void |
26 | warnx(format ": %s", ## __VA_ARGS__, strerror(errno)) | 38 | errx(int eval, const char *fmt, ...) |
39 | { | ||
40 | va_list ap; | ||
27 | 41 | ||
28 | #define warnx(format, ...) \ | 42 | va_start(ap, fmt); |
29 | fprintf(stderr, format "\n", ## __VA_ARGS__) | 43 | if (fmt != NULL) |
44 | vfprintf(stderr, fmt, ap); | ||
45 | fprintf(stderr, "\n"); | ||
46 | exit(eval); | ||
47 | va_end(ap); | ||
48 | } | ||
49 | |||
50 | static inline void | ||
51 | warn(const char *fmt, ...) | ||
52 | { | ||
53 | int sverrno = errno; | ||
54 | va_list ap; | ||
55 | |||
56 | va_start(ap, fmt); | ||
57 | if (fmt != NULL) { | ||
58 | vfprintf(stderr, fmt, ap); | ||
59 | fprintf(stderr, ": "); | ||
60 | } | ||
61 | fprintf(stderr, "%s\n", strerror(sverrno)); | ||
62 | va_end(ap); | ||
63 | } | ||
64 | |||
65 | static inline void | ||
66 | warnx(const char *fmt, ...) | ||
67 | { | ||
68 | va_list ap; | ||
69 | |||
70 | va_start(ap, fmt); | ||
71 | if (fmt != NULL) | ||
72 | vfprintf(stderr, fmt, ap); | ||
73 | fprintf(stderr, "\n"); | ||
74 | va_end(ap); | ||
75 | } | ||
30 | 76 | ||
31 | #endif | 77 | #endif |
32 | 78 | ||