Mostly stolen (and simplified) from git's implementation.
QE5NVXPUTYQD44LB7P3TK4LAZVJ4PVKQUUE6BX6KB32SGTYAG7HAC
/**
* Expand to support varargs like a regular printf
*/
static void
die_routine(const char *err, va_list params)
{
char msg[4096];
char *p, *pend;
pend = msg + sizeof(msg);
memcpy(msg, "fatal: ", 7); /* FIXME: make configurable */
p = msg + 7;
if (vsnprintf(p, (size_t)(pend - p), err, params) < 0)
*p = '\0'; /* vsnprintf() failed, clip at prefix */
for (; p != pend - 1 && *p; p++)
if (iscntrl(*p) && *p != '\t' && *p != '\n')
*p = '?';
*(p++) = '\n';
*(p++) = '\0';
fflush(stderr);
fprintf(stderr, msg); /* git has write_in_full(2, msg, p - msg) */
}