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 "xmalloc.h"
10 #include <sys/types.h>
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 #if defined(STDC_HEADERS)
15 #include <stdlib.h>
16 #endif
17 #include "fetchmail.h"
18 #include "i18n.h"
19
20 XMALLOCTYPE *
xmalloc(size_t n)21 xmalloc (size_t n)
22 {
23 XMALLOCTYPE *p;
24
25 p = (XMALLOCTYPE *) malloc(n);
26 if (p == (XMALLOCTYPE *) 0)
27 {
28 report(stderr, GT_("malloc failed\n"));
29 abort();
30 }
31 return(p);
32 }
33
34 XMALLOCTYPE *
xrealloc(XMALLOCTYPE * p,size_t n)35 xrealloc (XMALLOCTYPE *p, size_t n)
36 {
37 if (p == 0)
38 return xmalloc (n);
39 p = (XMALLOCTYPE *) realloc(p, n);
40 if (p == (XMALLOCTYPE *) 0)
41 {
42 report(stderr, GT_("realloc failed\n"));
43 abort();
44 }
45 return p;
46 }
47
xstrdup(const char * s)48 char *xstrdup(const char *s)
49 {
50 char *p;
51 p = (char *) xmalloc(strlen(s)+1);
52 strcpy(p,s);
53 return p;
54 }
55
56 #if !defined(HAVE_STRDUP)
strdup(const char * s)57 char *strdup(const char *s)
58 {
59 char *p;
60 p = (char *) malloc(strlen(s)+1);
61 if (p)
62 strcpy(p,s);
63 return p;
64 }
65 #endif /* !HAVE_STRDUP */
66
xstrndup(const char * s,size_t len)67 char *xstrndup(const char *s, size_t len)
68 {
69 char *p;
70 size_t l = strlen(s);
71
72 if (len < l) l = len;
73 p = (char *)xmalloc(l + 1);
74 memcpy(p, s, l);
75 p[l] = '\0';
76 return p;
77 }
78
79 /* xmalloc.c ends here */
80