vasprintf.c revision 71579
1176294Srwatson/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */ 2176294Srwatson 3176294Srwatson/* 4176294Srwatson * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 5176294Srwatson * All rights reserved. 6176294Srwatson * 7176294Srwatson * Redistribution and use in source and binary forms, with or without 8176294Srwatson * modification, are permitted provided that the following conditions 9176294Srwatson * are met: 10176294Srwatson * 1. Redistributions of source code must retain the above copyright 11176294Srwatson * notice, this list of conditions and the following disclaimer. 12176294Srwatson * 2. Redistributions in binary form must reproduce the above copyright 13176294Srwatson * notice, this list of conditions and the following disclaimer in the 14176294Srwatson * documentation and/or other materials provided with the distribution. 15176294Srwatson * 3. The name of the author may not be used to endorse or promote products 16176294Srwatson * derived from this software without specific prior written permission. 17176294Srwatson * 18176294Srwatson * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19176294Srwatson * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20176294Srwatson * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21176294Srwatson * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22176294Srwatson * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23176294Srwatson * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24176294Srwatson * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25176294Srwatson * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26176294Srwatson * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27176294Srwatson * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28176294Srwatson */ 29176294Srwatson 30176294Srwatson#if defined(LIBC_RCS) && !defined(lint) 31176294Srwatsonstatic char rcsid[] = "$FreeBSD: head/lib/libc/stdio/vasprintf.c 71579 2001-01-24 13:01:12Z deischen $"; 32176294Srwatson#endif /* LIBC_RCS and not lint */ 33176294Srwatson 34176294Srwatson#include <stdio.h> 35176294Srwatson#include <stdlib.h> 36176294Srwatson#include <errno.h> 37176294Srwatson#include "local.h" 38176294Srwatson 39176294Srwatsonint 40176294Srwatsonvasprintf(str, fmt, ap) 41176294Srwatson char **str; 42176294Srwatson const char *fmt; 43176294Srwatson _BSD_VA_LIST_ ap; 44176294Srwatson{ 45176294Srwatson int ret; 46176294Srwatson FILE f; 47176294Srwatson 48176294Srwatson f._file = -1; 49176294Srwatson f._flags = __SWR | __SSTR | __SALC; 50176294Srwatson f._bf._base = f._p = (unsigned char *)malloc(128); 51176294Srwatson if (f._bf._base == NULL) { 52176294Srwatson *str = NULL; 53176294Srwatson errno = ENOMEM; 54176294Srwatson return (-1); 55176294Srwatson } 56176294Srwatson f._bf._size = f._w = 127; /* Leave room for the NULL */ 57176294Srwatson ret = __vfprintf(&f, fmt, ap); 58176294Srwatson *f._p = '\0'; 59176294Srwatson f._bf._base = reallocf(f._bf._base, f._bf._size + 1); 60176294Srwatson if (f._bf._base == NULL) { 61176294Srwatson errno = ENOMEM; 62176294Srwatson ret = -1; 63176294Srwatson } 64176294Srwatson *str = (char *)f._bf._base; 65176294Srwatson return (ret); 66176294Srwatson} 67176294Srwatson