1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
/*
* xmalloc.c
*
* Code borrowed from util-linux-2.12r/mount/xmalloc.c
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#if HAVE_STRING_H
#include <string.h> /* strdup() */
#endif /* HAVE_STRING_H */
#include "xmalloc.h"
#include "nls.h" /* _() */
#include "sundries.h" /* EX_SYSERR */
void (*at_die)(void) = NULL;
/* Fatal error. Print message and exit. */
void
die(int err, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
if (at_die)
(*at_die)();
exit(err);
}
static void
die_if_null(void *t) {
if (t == NULL)
die(EX_SYSERR, _("not enough memory"));
}
void *xmalloc(size_t size)
{
void *t;
if (size == 0)
return NULL;
t = malloc(size);
die_if_null(t);
return t;
}
void *xrealloc(void *p, size_t size)
{
void *t;
t = realloc(p, size);
die_if_null(t);
return t;
}
char *xstrdup(const char *s)
{
char *t;
if (s == NULL)
return NULL;
t = strdup(s);
die_if_null(t);
return t;
}
|