/**
 * Error reporting functions.
 *
 * These functions are a replacement for <error.h> which is only available
 * in glibc - not in musl libc.
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
 * Output an error with a `va_list` to `stderr`.
 */
void
error_vformat(const char *format, va_list ap)
{
  vfprintf(stderr, format, ap);
  putc('\n', stderr);
  fflush(stderr);
}

/**
 * Output an error with variable arguments to `stderr` and exit with
 * a failure.
 */
void
error_format(const char *format, ...)
{
  va_list ap;
  va_start(ap, format);
  error_vformat(format, ap);
}

/**
 * Output an error with an error code to `stderr` and exit with a failure.
 */
void
error_code(int errnum, const char *format, ...)
{
  va_list ap;
  va_start(ap, format);
  vfprintf(stderr, format, ap);
  error_format(": %s", strerror(errnum));
}
