leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

critmem.c
Go to the documentation of this file.
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#ifndef _DEFAULT_SOURCE
26#define _DEFAULT_SOURCE
27#endif
28
29#ifndef _GNU_SOURCE
30#define _GNU_SOURCE
31#endif
32#ifndef _BSD_SOURCE
33#define _BSD_SOURCE
34#endif
35
36#include "critmem.h"
37
38#include <stdlib.h>
39#include <syslog.h>
40#include <string.h>
41#include <stdio.h>
42
43static char ebuf[1024]; /* error buffer */
44
45static int log_syslog = 1; /* log to syslog? */
46
47static void barf(const char *func, size_t size, const char *message) {
48 snprintf(ebuf, sizeof(ebuf), "%s(%ld) failed: %s", func, (long)size, message);
49 if (log_syslog)
50 syslog(LOG_ERR, "%s", ebuf);
51 fwrite(ebuf, strlen(ebuf), 1, stderr);
52 fwrite("\n", 1, 1, stderr);
53 exit(EXIT_FAILURE);
54}
55
56/*
57 * replacement for malloc, syslogs allocation failures
58 * and exits with the error message
59 */
60char *
61critmalloc(size_t size, const char *message)
62{
63 char *a;
64
65 a = (char *)malloc(size);
66 /* we don't want to return NULL, not even for size == 0 */
67 if (!a && size == 0)
68 a = (char *)malloc(1);
69 if (!a)
70 barf("malloc", size, message);
71 return a;
72}
73
74/*
75 * replacement for realloc, syslogs allocation failures
76 * and exits with the error message
77 */
78char *
79critrealloc(char *a, size_t size, const char *message)
80{
81 char *b = (char *)realloc(a, size);
82 if (!b && size != 0)
83 barf("realloc", size, message);
84 return b;
85}
86
87/*
88 * replacement for malloc, logs allocation failures
89 * and exits with the error message
90 */
91char *
92critstrdup(const char *source, const char *message)
93{
94 char *a;
95
96 a = (char *)critmalloc(strlen(source) + 1, message);
97 strcpy(a, source); /* RATS: ignore */
98 return a;
99}
100
101void critsyslog(int do_log) {
102 log_syslog = do_log;
103}
char * critrealloc(char *a, size_t size, const char *message)
Definition: critmem.c:79
char * critstrdup(const char *source, const char *message)
Definition: critmem.c:92
char * critmalloc(size_t size, const char *message)
Definition: critmem.c:61
void critsyslog(int do_log)
Definition: critmem.c:101
static int log_syslog
Definition: critmem.c:45
static void barf(const char *func, size_t size, const char *message)
Definition: critmem.c:47
static char ebuf[1024]
Definition: critmem.c:43
#define snprintf
Definition: leafnode.h:74