Reimplementation of Pijul in C, for education, fun and absolutely no profit
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include "scaffold.h"

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) */
}

void
die(const char *err, ...)
{
	va_list params;

	va_start(params, err);
	die_routine(err, params);
	va_end(params);

	exit(128);
}

void *
xmalloc(size_t size)
{
	void *ret;

	ret = malloc(size);
	if (!ret)
		die("Unable to allocate memory");

	memset(ret, 0, size);
	return ret;
}

void *
xrealloc(void *ptr, size_t size)
{
	void *ret;

	ret = realloc(ptr, size);
	if (!ret)
		die("Unable to allocate memory");
	return ret;
}

char *
xstrdup(const char *str)
{
	char *ret = strdup(str);
	if (!ret)
		die("out of memory, strdup failed");
	return ret;
}