Skip to content

Instantly share code, notes, and snippets.

@larryv
Last active May 2, 2016 00:16
Show Gist options
  • Save larryv/a1d7ac5cf21ed38454d1e5cb80140cb1 to your computer and use it in GitHub Desktop.
Save larryv/a1d7ac5cf21ed38454d1e5cb80140cb1 to your computer and use it in GitHub Desktop.
log message and exit
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
void die(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* This buffer length is arbitrary. */
const size_t buflen = 1024;
static char message[buflen] = "ERROR: ";
if (errno) {
vsnprintf(message, buflen, fmt, ap);
perror(message);
} else {
vsnprintf(strchr(message, '\0'), buflen - strlen(message), fmt, ap);
fprintf(stderr, "%s\n", message);
}
va_end(ap);
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment