Portable build framework for OpenNTPD
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.0 KiB

  1. /*
  2. * Public domain
  3. * err.h compatibility shim
  4. */
  5. #ifdef HAVE_ERR_H
  6. #include_next <err.h>
  7. #else
  8. #ifndef LIBCOMPAT_ERR_H
  9. #define LIBCOMPAT_ERR_H
  10. #include <errno.h>
  11. #include <stdarg.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. extern char *__progname;
  15. static inline void _warn(int err, const char *format, ...)
  16. __attribute__ ((__format__ (__printf__, 2, 3)));
  17. static inline void _warn(int err, const char *format, ...)
  18. {
  19. va_list args;
  20. va_start(args, format);
  21. fprintf(stderr, "%s: ", __progname);
  22. if (format != NULL) {
  23. vfprintf(stderr, format, args);
  24. if (err)
  25. fprintf(stderr, ": ");
  26. }
  27. if (err)
  28. fprintf(stderr, "%s", strerror(err));
  29. fprintf(stderr, "\n");
  30. va_end(args);
  31. }
  32. #define err(exitcode, format, args...) \
  33. do { warn(format, ## args); exit(exitcode); } while (0)
  34. #define errx(exitcode, format, args...) \
  35. do { warnx(format, ## args); exit(exitcode); } while (0)
  36. #define warn(format, args...) \
  37. _warn(errno, format, ## args)
  38. #define warnx(format, args...) \
  39. _warn(0, format, ## args)
  40. #endif
  41. #endif