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