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