/***** * * Copyright (C) 2004, 2005 Pablo Belin * All Rights Reserved * * This file is part of the gprelude program. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. * *****/ #include #include #include #include #include #include "gprelude-mem.h" int gprelude_free ( void *buff ) { if ( buff ) { free ( buff ); return 0; } return -1; } char *gprelude_vsprintf ( const char *fmt, va_list ap ) { va_list aq; char *str = NULL, *str_tmp; size_t size = BUFFSIZE_MIN; int n; while ( 1 ) { str_tmp = realloc ( str, size * sizeof(char) ); if ( !str_tmp ) { log ( LOG_ERR, "memory exhausted.\n" ); gprelude_free ( str ); return NULL; } str = str_tmp; memset ( str, 0, size * sizeof(char) ); va_copy ( aq, ap ); n = vsnprintf ( str, size, fmt, aq ); va_end ( aq ); if ( (-1 < n) && (n < size) ) return str; if ( n == -1 ) size += BUFFSIZE_STEP; else size = n + 1; } return NULL; /* nerver reached */ } char *gprelude_sprintf ( const char *fmt, ... ) { va_list ap; char *str; if ( !fmt ) return NULL; va_start ( ap, fmt ); str = gprelude_vsprintf ( fmt, ap ); va_end ( ap ); return str; } static char *__gprelude_strndup ( const char *str, int length ) { char *buff; buff = malloc ( (length + 1) * sizeof(char) ); if ( !buff ) { log ( LOG_ERR, "memory exhausted.\n" ); return NULL; } buff [ length ] = '\0'; strncpy ( buff, str, length ); return buff; } char *gprelude_strndup ( const char *str, int length ) { if ( !str ) return NULL; return __gprelude_strndup ( str, length ); } char *gprelude_strdup ( const char *str ) { if ( !str ) return NULL; return __gprelude_strndup ( str, strlen ( str ) ); }