]> Pileus Git - ~andy/fetchmail/blob - xmalloc.c
0ce0905330b3c805eb8bb3d55da5e0cfd9d91bd6
[~andy/fetchmail] / xmalloc.c
1 /*
2  * xmalloc.c -- allocate space or die 
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8 #include <stdio.h>
9 #include <errno.h>
10 #include <string.h>
11 #if defined(STDC_HEADERS)
12 #include  <stdlib.h>
13 #endif
14 #include "fetchmail.h"
15
16 #if defined(HAVE_VOIDPOINTER)
17 #define XMALLOCTYPE void
18 #else
19 #define XMALLOCTYPE char
20 #endif
21
22 XMALLOCTYPE *
23 xmalloc (int n)
24 {
25     XMALLOCTYPE *p;
26
27     p = (XMALLOCTYPE *) malloc(n);
28     if (p == (XMALLOCTYPE *) 0)
29         error(PS_UNDEFINED, errno, "malloc failed");
30     return(p);
31 }
32
33 XMALLOCTYPE *
34 xrealloc (XMALLOCTYPE *p, int n)
35 {
36     if (p == 0)
37         return xmalloc (n);
38     p = (XMALLOCTYPE *) realloc(p, n);
39     if (p == (XMALLOCTYPE *) 0)
40         error(PS_UNDEFINED, errno, "realloc failed");
41     return p;
42 }
43
44 char *xstrdup(const char *s)
45 {
46     char *p;
47     p = (char *) xmalloc(strlen(s)+1);
48     strcpy(p,s);
49     return p;
50 }
51
52 /* xmalloc.c ends here */