]> Pileus Git - ~andy/fetchmail/blob - xmalloc.c
Merge branch 'legacy_63'
[~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 <sys/types.h>
10 #include <stdio.h>
11 #include <errno.h>
12 #include <string.h>
13 #include  <stdlib.h>
14 #include "fetchmail.h"
15 #include "gettext.h"
16
17 void *xmalloc (size_t n)
18 {
19     void *p;
20
21     p = (void *) malloc(n);
22     if (p == (void *) 0)
23     {
24         report(stderr, GT_("malloc failed\n"));
25         abort();
26     }
27     return(p);
28 }
29
30 void *xrealloc (void *p, size_t n)
31 {
32     if (p == 0)
33         return xmalloc (n);
34     p = (void *) realloc(p, n);
35     if (p == (void *) 0)
36     {
37         report(stderr, GT_("realloc failed\n"));
38         abort();
39     }
40     return p;
41 }
42
43 char *xstrdup(const char *s)
44 {
45     char *p;
46     p = (char *) xmalloc(strlen(s)+1);
47     strcpy(p,s);
48     return p;
49 }
50
51 char *xstrndup(const char *s, size_t len)
52 {
53     char *p;
54     size_t l = strlen(s);
55
56     if (len < l) l = len;
57     p = (char *)xmalloc(l + 1);
58     memcpy(p, s, l);
59     p[l] = '\0';
60     return p;
61 }
62
63
64 /* xmalloc.c ends here */