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
907 B
55 lines
907 B
#define _POSIX_C_SOURCE 200809l
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <errno.h>
|
|
|
|
#include "err.h"
|
|
|
|
void die(const char *fmt, ...)
|
|
{
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
if (fmt[0] && fmt[strlen(fmt) - 1] == ':')
|
|
fprintf(stderr, " %s", strerror(errno));
|
|
else
|
|
fputc('\n', stderr);
|
|
|
|
va_end(ap);
|
|
exit(errno ? errno : EXIT_FAILURE);
|
|
}
|
|
|
|
void *emalloc(size_t s)
|
|
{
|
|
if (!s || s == (size_t)-1)
|
|
die("bad malloc: invalid size");
|
|
void *p = malloc(s);
|
|
if (!p)
|
|
die("bad malloc:");
|
|
return p;
|
|
}
|
|
|
|
void *erealloc(void *p, size_t s)
|
|
{
|
|
if (!s || s == (size_t)-1)
|
|
die("bad realloc: invalid size");
|
|
void *r = realloc(p, s);
|
|
if (!r)
|
|
die("bad realloc:");
|
|
return r;
|
|
}
|
|
|
|
char *estrdup(const char *s)
|
|
{
|
|
if (!s)
|
|
die("bad strdup: cannot duplicate NULL pointer");
|
|
char *r = strdup(s);
|
|
if (!r)
|
|
die("bad strdup:");
|
|
return r;
|
|
}
|
|
|