From 07e541cc2e188bfed9f3284471cfcd02e09f6983 Mon Sep 17 00:00:00 2001 From: Brent Cook Date: Sun, 3 Jan 2016 19:05:05 -0600 Subject: [PATCH] replace err.h macros with inline functions Passing NULL for the format is just easier with a function. --- include/compat/err.h | 62 ++++++++++++++++++++++++++++++++++++++------ 1 file 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 @@ #define LIBCRYPTOCOMPAT_ERR_H #include +#include +#include #include #include -#define err(exitcode, format, ...) \ - errx(exitcode, format ": %s", ## __VA_ARGS__, strerror(errno)) +static inline void +err(int eval, const char *fmt, ...) +{ + int sverrno = errno; + va_list ap; -#define errx(exitcode, format, ...) \ - do { warnx(format, ## __VA_ARGS__); exit(exitcode); } while (0) + va_start(ap, fmt); + if (fmt != NULL) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": "); + } + fprintf(stderr, "%s\n", strerror(sverrno)); + exit(eval); + va_end(ap); +} -#define warn(format, ...) \ - warnx(format ": %s", ## __VA_ARGS__, strerror(errno)) +static inline void +errx(int eval, const char *fmt, ...) +{ + va_list ap; -#define warnx(format, ...) \ - fprintf(stderr, format "\n", ## __VA_ARGS__) + va_start(ap, fmt); + if (fmt != NULL) + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + exit(eval); + va_end(ap); +} + +static inline void +warn(const char *fmt, ...) +{ + int sverrno = errno; + va_list ap; + + va_start(ap, fmt); + if (fmt != NULL) { + vfprintf(stderr, fmt, ap); + fprintf(stderr, ": "); + } + fprintf(stderr, "%s\n", strerror(sverrno)); + va_end(ap); +} + +static inline void +warnx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + if (fmt != NULL) + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + va_end(ap); +} #endif