jaromail

a commandline tool to easily and privately handle your e-mail
git clone git://parazyd.org/jaromail.git
Log | Files | Refs | Submodules | README

helpers.c (2387B)


      1 /*
      2  * The functions in this file have completely been stolen
      3  * from the Mutt mail user agent, with some slight
      4  * modifications by Thomas Roessler <roessler@guug.de> to
      5  * use them in the "little brother database".
      6  *
      7  * The following copyright notice applies:
      8  *
      9  * Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
     10  *
     11  *     This program is free software; you can
     12  *     redistribute it and/or modify it under the terms
     13  *     of the GNU General Public License as published by
     14  *     the Free Software Foundation; either version 2 of
     15  *     the License, or (at your option) any later
     16  *     version.
     17  *
     18  *     This program is distributed in the hope that it
     19  *     will be useful, but WITHOUT ANY WARRANTY; without
     20  *     even the implied warranty of MERCHANTABILITY or
     21  *     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     22  *     General Public License for more details.
     23  *
     24  *     You should have received a copy of the GNU General Public License
     25  *     along with this program; if not, write to the Free Software Foundation,
     26  *     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,, USA.
     27  */
     28 
     29 /* $Id: helpers.c,v 1.5 2005-10-29 14:48:08 roland Exp $ */
     30 
     31 #include <stdio.h>
     32 #include <stdlib.h>
     33 #include <string.h>
     34 #include <unistd.h>
     35 
     36 #include "helpers.h"
     37 
     38 
     39 void *safe_calloc (size_t nmemb, size_t size)
     40 {
     41   void *p;
     42 
     43   if (!nmemb || !size)
     44     return NULL;
     45   if (!(p = calloc (nmemb, size)))
     46   {
     47     perror ("Out of memory");
     48     sleep (1);
     49     exit (1);
     50   }
     51   return p;
     52 }
     53 
     54 void *safe_malloc (unsigned int siz)
     55 {
     56   void *p;
     57 
     58   if (siz == 0)
     59     return 0;
     60   if ((p = (void *) malloc (siz)) == 0)
     61   {
     62     perror ("Out of memory!");
     63     sleep (1);
     64     exit (1);
     65   }
     66   return (p);
     67 }
     68 
     69 void safe_realloc (void **p, size_t siz)
     70 {
     71   void *r;
     72 
     73   if (siz == 0)
     74   {
     75     if (*p)
     76     {
     77       free (*p);
     78       *p = NULL;
     79     }
     80     return;
     81   }
     82 
     83   if (*p)
     84     r = (void *) realloc (*p, siz);
     85   else
     86   {
     87     /* realloc(NULL, nbytes) doesn't seem to work under SunOS 4.1.x */
     88     r = (void *) malloc (siz);
     89   }
     90 
     91   if (!r)
     92   {
     93     perror ("Out of memory!");
     94     sleep (1);
     95     exit (1);
     96   }
     97 
     98   *p = r;
     99 }
    100 
    101 void safe_free (void *ptr)
    102 { 
    103   void **p = (void **)ptr;
    104   if (*p)
    105   { 
    106     free (*p);
    107     *p = 0;
    108   }
    109 }
    110 
    111 char *safe_strdup (const char *s)
    112 {
    113   char *p;
    114   size_t l;
    115 
    116   if (!s || !*s) return 0;
    117   l = strlen (s) + 1;
    118   p = (char *)safe_malloc (l);
    119   memcpy (p, s, l);
    120   return (p);
    121 }
    122