Main Page | Data Structures | Directories | File List | Data Fields

src/safe.c

00001 /********************************************************************\
00002  * This program is free software; you can redistribute it and/or    *
00003  * modify it under the terms of the GNU General Public License as   *
00004  * published by the Free Software Foundation; either version 2 of   *
00005  * the License, or (at your option) any later version.              *
00006  *                                                                  *
00007  * This program is distributed in the hope that it will be useful,  *
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00010  * GNU General Public License for more details.                     *
00011  *                                                                  *
00012  * You should have received a copy of the GNU General Public License*
00013  * along with this program; if not, contact:                        *
00014  *                                                                  *
00015  * Free Software Foundation           Voice:  +1-617-542-5942       *
00016  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00017  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00018  *                                                                  *
00019  \********************************************************************/
00020 
00021 /*
00022  * $Header: /cvsroot/wifidog/wifidog/src/safe.c,v 1.1 2005/02/20 21:29:15 minaguib Exp $
00023  */
00030 #include <stdarg.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <errno.h>
00034 
00035 #include "safe.h"
00036 #include "debug.h"
00037 #include <syslog.h>
00038 
00039 void * safe_malloc (size_t size) {
00040         void * retval = NULL;
00041         retval = malloc(size);
00042         if (!retval) {
00043                 debug(LOG_CRIT, "Failed to malloc %d bytes of memory: %s.  Bailing out", size, strerror(errno));
00044                 exit(1);
00045         }
00046         return (retval);
00047 }
00048 
00049 char * safe_strdup(const char *s) {
00050         char * retval = NULL;
00051         if (!s) {
00052                 debug(LOG_CRIT, "safe_strdup called with NULL which would have crashed strdup. Bailing out");
00053                 exit(1);
00054         }
00055         retval = strdup(s);
00056         if (!retval) {
00057                 debug(LOG_CRIT, "Failed to duplicate a string: %s.  Bailing out", strerror(errno));
00058                 exit(1);
00059         }
00060         return (retval);
00061 }
00062 
00063 int safe_asprintf(char **strp, const char *fmt, ...) {
00064         va_list ap;
00065         int retval;
00066 
00067         va_start(ap, fmt);
00068         retval = safe_vasprintf(strp, fmt, ap);
00069         va_end(ap);
00070 
00071         return (retval);
00072 }
00073 
00074 int safe_vasprintf(char **strp, const char *fmt, va_list ap) {
00075         int retval;
00076 
00077         retval = vasprintf(strp, fmt, ap);
00078 
00079         if (retval == -1) {
00080                 debug(LOG_CRIT, "Failed to vasprintf: %s.  Bailing out", strerror(errno));
00081                 exit (1);
00082         }
00083         return (retval);
00084 }

Generated on Sun Apr 3 20:04:46 2005 for WifiDog by  doxygen 1.4.1