1104001Stjr/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */ 237487Speter 337487Speter/* 437487Speter * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 537487Speter * All rights reserved. 615931Speter * 7227753Stheraven * Copyright (c) 2011 The FreeBSD Foundation 8227753Stheraven * All rights reserved. 9227753Stheraven * Portions of this software were developed by David Chisnall 10227753Stheraven * under sponsorship from the FreeBSD Foundation. 11227753Stheraven * 1215931Speter * Redistribution and use in source and binary forms, with or without 1315931Speter * modification, are permitted provided that the following conditions 1415931Speter * are met: 1515931Speter * 1. Redistributions of source code must retain the above copyright 1615931Speter * notice, this list of conditions and the following disclaimer. 1715931Speter * 2. Redistributions in binary form must reproduce the above copyright 1815931Speter * notice, this list of conditions and the following disclaimer in the 1915931Speter * documentation and/or other materials provided with the distribution. 2037487Speter * 3. The name of the author may not be used to endorse or promote products 2137487Speter * derived from this software without specific prior written permission. 2215931Speter * 2337487Speter * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 2437487Speter * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 2537487Speter * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 2637487Speter * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 2737487Speter * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 2837487Speter * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 2937487Speter * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 3037487Speter * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 3137487Speter * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 3237487Speter * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3315931Speter */ 3415931Speter 3592986Sobrien#include <sys/cdefs.h> 3692986Sobrien__FBSDID("$FreeBSD$"); 3715931Speter 3815931Speter#include <stdio.h> 3915931Speter#include <stdlib.h> 4037487Speter#include <errno.h> 41227753Stheraven#include "xlocale_private.h" 4271579Sdeischen#include "local.h" 4315931Speter 4415931Speterint 45227753Stheravenvasprintf_l(char **str, locale_t locale, const char *fmt, __va_list ap) 4615931Speter{ 47206217Sobrien FILE f = FAKE_FILE; 4815931Speter int ret; 49227753Stheraven FIX_LOCALE(locale); 5015931Speter 5137487Speter f._flags = __SWR | __SSTR | __SALC; 52206217Sobrien f._bf._base = f._p = malloc(128); 53104001Stjr if (f._bf._base == NULL) { 54104001Stjr *str = NULL; 55104001Stjr errno = ENOMEM; 56104001Stjr return (-1); 57104001Stjr } 58102085Sjmallett f._bf._size = f._w = 127; /* Leave room for the NUL */ 59227753Stheraven ret = __vfprintf(&f, locale, fmt, ap); 60104001Stjr if (ret < 0) { 61104001Stjr free(f._bf._base); 62104001Stjr *str = NULL; 63104001Stjr errno = ENOMEM; 64104001Stjr return (-1); 65104001Stjr } 6637487Speter *f._p = '\0'; 67104001Stjr *str = (char *)f._bf._base; 68103996Stjr return (ret); 6915931Speter} 70227753Stheravenint 71227753Stheravenvasprintf(char **str, const char *fmt, __va_list ap) 72227753Stheraven{ 73227753Stheraven return vasprintf_l(str, __get_locale(), fmt, ap); 74227753Stheraven} 75