1 /*
2  * critmem.c -- memory allocation that only returns on success
3  *
4  * (C) 2000 - 2002 by Matthias Andree <matthias.andree@gmx.de>
5  * Backport of critstrdup courtesy of Ralf Wildenhues <ralf.wildenhues@gmx.de>
6  *
7  * ---------------------------------------------------------------------------
8  *
9  * This library is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2 or 2.1 of
12  * the License. See the file COPYING.LGPL for details.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  */
24 
25 #include "critmem.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <syslog.h>
29 #include <string.h>
30 
31 static char ebuf[1024]; /* error buffer */
32 
33 static int log_syslog = 1; /* log to syslog? */
34 
barf(const char * func,size_t size,const char * message)35 static void barf(const char *func, size_t size, const char *message) {
36     snprintf(ebuf, sizeof(ebuf), "%s(%ld) failed: %s", func, (long)size, message);
37     if (log_syslog)
38 	syslog(LOG_ERR, "%s", ebuf);
39     fwrite(ebuf, strlen(ebuf), 1, stderr);
40     fwrite("\n", 1, 1, stderr);
41     exit(EXIT_FAILURE);
42 }
43 
44 /*
45  * replacement for malloc, syslogs allocation failures
46  * and exits with the error message
47  */
48 char *
critmalloc(size_t size,const char * message)49 critmalloc(size_t size, const char *message)
50 {
51     char *a;
52 
53     a = (char *)malloc(size);
54     /* we don't want to return NULL, not even for size == 0 */
55     if (!a && size == 0)
56 	a = (char *)malloc(1);
57     if (!a)
58 	barf("malloc", size, message);
59     return a;
60 }
61 
62 /*
63  * replacement for realloc, syslogs allocation failures
64  * and exits with the error message
65  */
66 char *
critrealloc(char * a,size_t size,const char * message)67 critrealloc(char *a, size_t size, const char *message)
68 {
69     char *b = (char *)realloc(a, size);
70     if (!b && size != 0)
71 	barf("realloc", size, message);
72     return b;
73 }
74 
75 /*
76  * replacement for malloc, logs allocation failures
77  * and exits with the error message
78  */
79 char *
critstrdup(const char * source,const char * message)80 critstrdup(const char *source, const char *message)
81 {
82     char *a;
83 
84     a = (char *)critmalloc(strlen(source) + 1, message);
85     strcpy(a, source);		/* RATS: ignore */
86     return a;
87 }
88 
critsyslog(int do_log)89 void critsyslog(int do_log) {
90     log_syslog = do_log;
91 }
92