"Fossies" - the Fresh Open Source Software Archive

Member "leafnode-1.12.0/critmem.c" (28 Dec 2021, 2686 Bytes) of package /linux/misc/leafnode-1.12.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "critmem.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.11.12_vs_1.12.0.

    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 
   43 static char ebuf[1024]; /* error buffer */
   44 
   45 static int log_syslog = 1; /* log to syslog? */
   46 
   47 static 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  */
   60 char *
   61 critmalloc(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  */
   78 char *
   79 critrealloc(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  */
   91 char *
   92 critstrdup(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 
  101 void critsyslog(int do_log) {
  102     log_syslog = do_log;
  103 }