vasprintf.c revision 104001
1176227Sbde/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */ 2176227Sbde 3176227Sbde/* 4176227Sbde * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 5176227Sbde * All rights reserved. 6176227Sbde * 7176227Sbde * Redistribution and use in source and binary forms, with or without 8176227Sbde * modification, are permitted provided that the following conditions 9176227Sbde * are met: 10176227Sbde * 1. Redistributions of source code must retain the above copyright 11176227Sbde * notice, this list of conditions and the following disclaimer. 12176227Sbde * 2. Redistributions in binary form must reproduce the above copyright 13176227Sbde * notice, this list of conditions and the following disclaimer in the 14176227Sbde * documentation and/or other materials provided with the distribution. 15176227Sbde * 3. The name of the author may not be used to endorse or promote products 16176227Sbde * derived from this software without specific prior written permission. 17176227Sbde * 18176227Sbde * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 19176227Sbde * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 20176227Sbde * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21176227Sbde * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22176227Sbde * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23176227Sbde * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 24176227Sbde * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25176227Sbde * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26176227Sbde * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 27176227Sbde * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28176227Sbde */ 29176227Sbde 30176227Sbde#include <sys/cdefs.h> 31176227Sbde__FBSDID("$FreeBSD: head/lib/libc/stdio/vasprintf.c 104001 2002-09-26 13:11:24Z tjr $"); 32176227Sbde 33176227Sbde#include <stdio.h> 34176227Sbde#include <stdlib.h> 35176227Sbde#include <errno.h> 36176227Sbde#include "local.h" 37176227Sbde 38176227Sbdeint 39176227Sbdevasprintf(str, fmt, ap) 40176227Sbde char **str; 41176227Sbde const char *fmt; 42176227Sbde __va_list ap; 43176227Sbde{ 44176227Sbde int ret; 45176227Sbde FILE f; 46176227Sbde struct __sFILEX ext; 47176227Sbde 48176227Sbde f._file = -1; 49176227Sbde f._flags = __SWR | __SSTR | __SALC; 50176227Sbde f._bf._base = f._p = (unsigned char *)malloc(128); 51176227Sbde if (f._bf._base == NULL) { 52176227Sbde *str = NULL; 53176227Sbde errno = ENOMEM; 54217108Skib return (-1); 55217108Skib } 56 f._bf._size = f._w = 127; /* Leave room for the NUL */ 57 f._extra = &ext; 58 INITEXTRA(&f); 59 ret = __vfprintf(&f, fmt, ap); 60 if (ret < 0) { 61 free(f._bf._base); 62 *str = NULL; 63 errno = ENOMEM; 64 return (-1); 65 } 66 *f._p = '\0'; 67 *str = (char *)f._bf._base; 68 return (ret); 69} 70