replace err.h macros with inline functions

Passing NULL for the format is just easier with a function.
This commit is contained in:
Brent Cook 2016-01-03 19:05:05 -06:00
parent cf86bf8581
commit 07e541cc2e

View File

@ -13,20 +13,66 @@
#define LIBCRYPTOCOMPAT_ERR_H #define LIBCRYPTOCOMPAT_ERR_H
#include <errno.h> #include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define err(exitcode, format, ...) \ static inline void
errx(exitcode, format ": %s", ## __VA_ARGS__, strerror(errno)) err(int eval, const char *fmt, ...)
{
int sverrno = errno;
va_list ap;
#define errx(exitcode, format, ...) \ va_start(ap, fmt);
do { warnx(format, ## __VA_ARGS__); exit(exitcode); } while (0) if (fmt != NULL) {
vfprintf(stderr, fmt, ap);
fprintf(stderr, ": ");
}
fprintf(stderr, "%s\n", strerror(sverrno));
exit(eval);
va_end(ap);
}
#define warn(format, ...) \ static inline void
warnx(format ": %s", ## __VA_ARGS__, strerror(errno)) errx(int eval, const char *fmt, ...)
{
va_list ap;
#define warnx(format, ...) \ va_start(ap, fmt);
fprintf(stderr, format "\n", ## __VA_ARGS__) 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 #endif