vasprintf.c revision 302408
1145132Sanholt/*	$OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $	*/
2145132Sanholt
3145132Sanholt/*
4145132Sanholt * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5145132Sanholt * All rights reserved.
6145132Sanholt *
7145132Sanholt * Copyright (c) 2011 The FreeBSD Foundation
8145132Sanholt * All rights reserved.
9145132Sanholt * Portions of this software were developed by David Chisnall
10145132Sanholt * under sponsorship from the FreeBSD Foundation.
11145132Sanholt *
12145132Sanholt * Redistribution and use in source and binary forms, with or without
13145132Sanholt * modification, are permitted provided that the following conditions
14145132Sanholt * are met:
15145132Sanholt * 1. Redistributions of source code must retain the above copyright
16145132Sanholt *    notice, this list of conditions and the following disclaimer.
17145132Sanholt * 2. Redistributions in binary form must reproduce the above copyright
18145132Sanholt *    notice, this list of conditions and the following disclaimer in the
19145132Sanholt *    documentation and/or other materials provided with the distribution.
20145132Sanholt * 3. The name of the author may not be used to endorse or promote products
21145132Sanholt *    derived from this software without specific prior written permission.
22145132Sanholt *
23145132Sanholt * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24145132Sanholt * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25145132Sanholt * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
26145132Sanholt * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27145132Sanholt * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28145132Sanholt * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29145132Sanholt * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30145132Sanholt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31145132Sanholt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32152909Sanholt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33152909Sanholt */
34152909Sanholt
35145132Sanholt#include <sys/cdefs.h>
36145132Sanholt__FBSDID("$FreeBSD: stable/11/lib/libc/stdio/vasprintf.c 227753 2011-11-20 14:45:42Z theraven $");
37145132Sanholt
38145132Sanholt#include <stdio.h>
39145132Sanholt#include <stdlib.h>
40145132Sanholt#include <errno.h>
41145132Sanholt#include "xlocale_private.h"
42145132Sanholt#include "local.h"
43145132Sanholt
44145132Sanholtint
45145132Sanholtvasprintf_l(char **str, locale_t locale, const char *fmt, __va_list ap)
46145132Sanholt{
47145132Sanholt	FILE f = FAKE_FILE;
48145132Sanholt	int ret;
49145132Sanholt	FIX_LOCALE(locale);
50145132Sanholt
51145132Sanholt	f._flags = __SWR | __SSTR | __SALC;
52145132Sanholt	f._bf._base = f._p = malloc(128);
53145132Sanholt	if (f._bf._base == NULL) {
54145132Sanholt		*str = NULL;
55145132Sanholt		errno = ENOMEM;
56145132Sanholt		return (-1);
57145132Sanholt	}
58145132Sanholt	f._bf._size = f._w = 127;		/* Leave room for the NUL */
59145132Sanholt	ret = __vfprintf(&f, locale, fmt, ap);
60145132Sanholt	if (ret < 0) {
61145132Sanholt		free(f._bf._base);
62145132Sanholt		*str = NULL;
63145132Sanholt		errno = ENOMEM;
64145132Sanholt		return (-1);
65145132Sanholt	}
66145132Sanholt	*f._p = '\0';
67145132Sanholt	*str = (char *)f._bf._base;
68145132Sanholt	return (ret);
69145132Sanholt}
70145132Sanholtint
71145132Sanholtvasprintf(char **str, const char *fmt, __va_list ap)
72145132Sanholt{
73145132Sanholt	return vasprintf_l(str, __get_locale(), fmt, ap);
74145132Sanholt}
75145132Sanholt