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 84 85 86 87 88 89 90 91 92
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "imapfilter.h"
/*
* A malloc() that checks the results and dies in case of error.
*/
void *
xmalloc(size_t size)
{
void *ptr;
ptr = (void *)malloc(size);
if (ptr == NULL)
fatal(ERROR_MEMALLOC,
"allocating memory; %s\n", strerror(errno));
return ptr;
}
/*
* A realloc() that checks the results and dies in case of error.
*/
void *
xrealloc(void *ptr, size_t size)
{
ptr = (void *)realloc(ptr, size);
if (ptr == NULL)
fatal(ERROR_MEMALLOC,
"allocating memory; %s\n", strerror(errno));
return ptr;
}
/*
* A free() that dies if fed with NULL pointer.
*/
void
xfree(void *ptr)
{
if (ptr == NULL)
fatal(ERROR_MEMALLOC,
"NULL pointer given as argument\n");
free(ptr);
}
/*
* A strdup() that checks the results and dies in case of error.
*/
char *
xstrdup(const char *str)
{
char *dup;
dup = strdup(str);
if (dup == NULL)
fatal(ERROR_MEMALLOC, "allocating memory; %s\n",
strerror(errno));
return dup;
}
/*
* A strndup() implementation that also checks the results and dies in case of
* error.
*/
char *
xstrndup(const char *str, size_t len)
{
char *dup;
dup = (char *)xmalloc((len + 1) * sizeof(char));
memcpy(dup, str, len);
dup[len] = '\0';
return dup;
}
|