Browse Source

Improve err.h macros.

Handle 'NULL', emit the program name.
OPENBSD_5_7
Brent Cook 10 years ago
parent
commit
f3d67df248
1 changed files with 27 additions and 5 deletions
  1. +27
    -5
      include/err.h

+ 27
- 5
include/err.h View File

@ -9,24 +9,46 @@
#else
#ifndef LIBCRYPTOCOMPAT_ERR_H
#define LIBCRYPTOCOMPAT_ERR_H
#ifndef LIBCOMPAT_ERR_H
#define LIBCOMPAT_ERR_H
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
extern char *__progname;
static inline _warn(int err, const char *format, ...)
__attribute__ ((__format__ (__printf__, 2, 3)));
static inline _warn(int err, const char *format, ...)
{
va_list args;
va_start(args, format);
fprintf(stderr, "%s: ", __progname);
if (format != NULL) {
vfprintf(stderr, format, args);
if (err)
fprintf(stderr, ": ");
}
if (err)
fprintf(stderr, "%s", strerror(err));
fprintf(stderr, "\n");
va_end(args);
}
#define err(exitcode, format, args...) \
errx(exitcode, format ": %s", ## args, strerror(errno))
do { warn(format, ## args); exit(exitcode); } while (0)
#define errx(exitcode, format, args...) \
do { warnx(format, ## args); exit(exitcode); } while (0)
#define warn(format, args...) \
warnx(format ": %s", ## args, strerror(errno))
_warn(errno, format, ## args)
#define warnx(format, args...) \
fprintf(stderr, format "\n", ## args)
_warn(0, format, ## args)
#endif


Loading…
Cancel
Save