1 /* -*-c-*- */
2 /* This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, see: <http://www.gnu.org/licenses/>
14  */
15 
16 #include "config.h"
17 
18 #include <stdio.h>
19 #include "safemalloc.h"
20 
alloc_failed(char * c,int length)21 static void alloc_failed(char *c, int length)
22 {
23 	fprintf(stderr, "%s of %d bytes failed. Exiting\n", c, length);
24 	exit(1);
25 }
26 
27 
28 /***********************************************************************
29  *
30  *  Procedure:
31  *      safemalloc - mallocs specified space or exits if there's a
32  *                   problem
33  *
34  ***********************************************************************/
safemalloc(int length)35 char *safemalloc(int length)
36 {
37 	char *ptr;
38 
39 	if(length <= 0)
40 		length = 1;
41 
42 	ptr = malloc(length);
43 	if(ptr == (char *)0)
44 	{
45 		/* doesn't return */
46 		alloc_failed("malloc", length);
47 	}
48 	return ptr;
49 }
50 
51 
52 /***********************************************************************
53  *
54  *  Procedure:
55  *      safecalloc - callocs specified space or exits if there's a
56  *                   problem
57  *
58  ***********************************************************************/
safecalloc(int num,int length)59 char *safecalloc(int num, int length)
60 {
61 	char *ptr;
62 
63 	ptr = calloc(num, length);
64 	if(ptr == (char *)0)
65 	{
66 		/* doesn't return */
67 		alloc_failed("calloc", length);
68 	}
69 	return ptr;
70 }
71 
72 
73 /***********************************************************************
74  *
75  *  Procedure:
76  *      saferealloc - reallocs specified space or exits if there's a
77  *                   problem
78  *
79  ***********************************************************************/
saferealloc(char * src,int length)80 char *saferealloc(char *src, int length)
81 {
82 	char *ptr;
83 
84 	if (src)
85 		ptr = realloc((void *)src, length);
86 	else
87 		ptr = malloc(length);
88 	if(ptr == (char *)0)
89 	{
90 		/* doesn't return */
91 		alloc_failed("realloc", length);
92 	}
93 	return ptr;
94 }
95 
96 /***********************************************************************
97  *
98  *  Procedure:
99  *      safestrdup - duplicates a string or exits if there's a
100  *                   problem
101  *
102  ***********************************************************************/
safestrdup(const char * src)103 char *safestrdup(const char *src)
104 {
105 	char *cpy = strdup(src);
106 	if (cpy == (char *)0)
107 	{
108 		/* doesn't return */
109 		alloc_failed("strdup", strlen(src) + (size_t)1);
110 	}
111 	return cpy;
112 }
113