"Fossies" - the Fresh Open Source Software Archive

Member "msmtp-1.8.5/src/xalloc.c" (13 Dec 2018, 2308 Bytes) of package /linux/privat/msmtp-1.8.5.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 "xalloc.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.8.1_vs_1.8.2.

    1 /*
    2  * xalloc.c
    3  *
    4  * This file is part of msmtp, an SMTP client, and of mpop, a POP3 client.
    5  *
    6  * Copyright (C) 2004, 2005, 2011, 2014, 2018
    7  * Martin Lambers <marlam@marlam.de>
    8  *
    9  *   This program is free software; you can redistribute it and/or modify
   10  *   it under the terms of the GNU General Public License as published by
   11  *   the Free Software Foundation; either version 3 of the License, or
   12  *   (at your option) any later version.
   13  *
   14  *   This program is distributed in the hope that it will be useful,
   15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17  *   GNU General Public License for more details.
   18  *
   19  *   You should have received a copy of the GNU General Public License
   20  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
   21  */
   22 
   23 #ifdef HAVE_CONFIG_H
   24 # include "config.h"
   25 #endif
   26 
   27 #include <stdio.h>
   28 #include <stdlib.h>
   29 #include <string.h>
   30 #include <limits.h>
   31 #include <stdarg.h>
   32 
   33 #include "xalloc.h"
   34 
   35 extern void xalloc_die(void);
   36 
   37 
   38 /*
   39  * xmalloc()
   40  */
   41 
   42 void *xmalloc(size_t size)
   43 {
   44     void *ptr;
   45 
   46     if (!(ptr = malloc(size)))
   47     {
   48         xalloc_die();
   49     }
   50     return ptr;
   51 }
   52 
   53 /*
   54  * xcalloc()
   55  */
   56 
   57 void *xcalloc(size_t n, size_t size)
   58 {
   59     void *ptr;
   60 
   61     if (!(ptr = calloc(n, size)))
   62     {
   63         xalloc_die();
   64     }
   65     return ptr;
   66 }
   67 
   68 /*
   69  * xrealloc()
   70  */
   71 
   72 void *xrealloc(void *ptr, size_t newsize)
   73 {
   74     if (!(ptr = realloc(ptr, newsize)))
   75     {
   76         xalloc_die();
   77     }
   78     return ptr;
   79 }
   80 
   81 /*
   82  * xstrdup()
   83  */
   84 
   85 char *xstrdup(const char *s)
   86 {
   87     char *p;
   88 
   89     if (!(p = strdup(s)))
   90     {
   91         xalloc_die();
   92     }
   93     return p;
   94 }
   95 
   96 /*
   97  * xstrndup()
   98  */
   99 
  100 char *xstrndup(const char *s, size_t n)
  101 {
  102 #ifdef HAVE_STRNDUP
  103     char *p;
  104 
  105     if (!(p = strndup(s, n)))
  106     {
  107         xalloc_die();
  108     }
  109     return p;
  110 #else
  111     size_t l = 0;
  112     char *p;
  113 
  114     while (l < n && s[l] != '\0')
  115     {
  116         l++;
  117     }
  118     p = malloc(l + 1);
  119     memcpy(p, s, l);
  120     p[l] = '\0';
  121     return p;
  122 #endif
  123 }
  124 
  125 /*
  126  * xasprintf()
  127  */
  128 
  129 char *xasprintf(const char *format, ...)
  130 {
  131     char *strp;
  132     int count;
  133     va_list args;
  134 
  135     va_start(args, format);
  136     count = vasprintf(&strp, format, args);
  137     if (count < 0)
  138     {
  139         xalloc_die();
  140     }
  141     va_end(args);
  142 
  143     return strp;
  144 }