00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org> 00002 // 00003 // This program is free software; you can redistribute it and/or 00004 // modify it under the terms of the GNU General Public License 00005 // as published by the Free Software Foundation; either version 2 00006 // of the License, or (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 00016 00017 #include "mem.h" 00018 #include "printf.h" 00019 00020 int _lucid_vasprintf(char **ptr, const char *fmt, va_list ap) 00021 { 00022 va_list ap2; 00023 int len; 00024 char *buf; 00025 00026 /* don't consume the original ap, we'll need it again */ 00027 va_copy(ap2, ap); 00028 00029 /* get required size */ 00030 len = _lucid_vsnprintf(0, 0, fmt, ap2); 00031 00032 va_end(ap2); 00033 00034 /* if size is 0, no buffer is allocated 00035 ** just set *ptr to NULL and return size */ 00036 if (len > 0) { 00037 if (!(buf = mem_alloc(len + 1))) 00038 return -1; 00039 00040 _lucid_vsnprintf(buf, len + 1, fmt, ap); 00041 00042 *ptr = buf; 00043 } 00044 00045 return len; 00046 }