From 15ff73b9843c79c1709f4a521aaa09285105f8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Fonseca?= Date: Thu, 3 Jul 2003 18:25:53 +0000 Subject: Exit with an error code immediately whenever an error. Extensive use of xmalloc and friends. --- xmalloc.h | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 xmalloc.h (limited to 'xmalloc.h') diff --git a/xmalloc.h b/xmalloc.h new file mode 100644 index 0000000..881bcad --- /dev/null +++ b/xmalloc.h @@ -0,0 +1,56 @@ +/** + * \file xmalloc.c + * Allocate space or die. + * + * \author Adapted from fetchmail's xmalloc.c by José Fonseca. + */ + +#ifndef _XMALLOC_H +#define _XMALLOC_H + + +#include +#include +#include + +#include "main.h" + + +static inline +void *xmalloc(size_t size) +{ + void *ptr; + + ptr = malloc(size); + if (ptr == NULL) + { + perror(NULL); + exit(EX_OSERR); + } + return(ptr); +} + +static inline +void *xrealloc(void *ptr, size_t size) +{ + if (ptr == 0) + return xmalloc(size); + ptr = realloc(ptr, size); + if (ptr == NULL) + { + perror(NULL); + exit(EX_OSERR); + } + return ptr; +} + +static inline +char *xstrdup(const char *s) +{ + char *p; + p = (char *)xmalloc(strlen(s) + 1); + strcpy(p, s); + return p; +} + +#endif -- cgit v1.2.3