diff options
author | Karel Zak <kzak@redhat.com> | 2006-12-07 00:26:31 +0100 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2006-12-07 00:26:31 +0100 |
commit | 756bfd018eb393640dad490df1a1ca840d9ee79b (patch) | |
tree | d95fd02eefcc647d4886761ccb74e976ee7aa32a /mount/xmalloc.c | |
parent | a47f2e66141271cde40ee5190acf93d7878bc93d (diff) | |
download | util-linux-old-756bfd018eb393640dad490df1a1ca840d9ee79b.tar.gz |
Imported from util-linux-2.12o tarball.
Diffstat (limited to 'mount/xmalloc.c')
-rw-r--r-- | mount/xmalloc.c | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/mount/xmalloc.c b/mount/xmalloc.c new file mode 100644 index 00000000..c3f51e50 --- /dev/null +++ b/mount/xmalloc.c @@ -0,0 +1,66 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> /* strdup() */ +#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; +} |