xref: /minix3/lib/libc/stdio/vasprintf.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: vasprintf.c,v 1.17 2013/05/19 21:45:00 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. The name of the author may not be used to endorse or promote products
162fe8fb19SBen Gras  *    derived from this software without specific prior written permission.
172fe8fb19SBen Gras  *
182fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
192fe8fb19SBen Gras  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
202fe8fb19SBen Gras  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
212fe8fb19SBen Gras  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
222fe8fb19SBen Gras  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
232fe8fb19SBen Gras  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
242fe8fb19SBen Gras  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
252fe8fb19SBen Gras  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
262fe8fb19SBen Gras  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
272fe8fb19SBen Gras  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
282fe8fb19SBen Gras  */
292fe8fb19SBen Gras 
302fe8fb19SBen Gras #include <sys/cdefs.h>
312fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
32*84d9c625SLionel Sambuc __RCSID("$NetBSD: vasprintf.c,v 1.17 2013/05/19 21:45:00 christos Exp $");
332fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
342fe8fb19SBen Gras 
35*84d9c625SLionel Sambuc #include "namespace.h"
36*84d9c625SLionel Sambuc 
372fe8fb19SBen Gras #include <assert.h>
382fe8fb19SBen Gras #include <errno.h>
39*84d9c625SLionel Sambuc #include <locale.h>
402fe8fb19SBen Gras #include <stdio.h>
412fe8fb19SBen Gras #include <stdlib.h>
42*84d9c625SLionel Sambuc 
432fe8fb19SBen Gras #include "reentrant.h"
44*84d9c625SLionel Sambuc #include "setlocale_local.h"
452fe8fb19SBen Gras #include "local.h"
462fe8fb19SBen Gras 
__weak_alias(asprintf,_asprintf)47*84d9c625SLionel Sambuc __weak_alias(asprintf, _asprintf)
48*84d9c625SLionel Sambuc __weak_alias(asprintf_l, _asprintf_l)
49*84d9c625SLionel Sambuc __weak_alias(vasprintf, _vasprintf)
50*84d9c625SLionel Sambuc __weak_alias(vasprintf_l, _vasprintf_l)
51*84d9c625SLionel Sambuc 
522fe8fb19SBen Gras int
53*84d9c625SLionel Sambuc vasprintf_l(char **str, locale_t loc, const char *fmt, va_list ap)
542fe8fb19SBen Gras {
552fe8fb19SBen Gras 	int ret;
562fe8fb19SBen Gras 	FILE f;
572fe8fb19SBen Gras 	struct __sfileext fext;
582fe8fb19SBen Gras 	unsigned char *_base;
592fe8fb19SBen Gras 
602fe8fb19SBen Gras 	_DIAGASSERT(str != NULL);
612fe8fb19SBen Gras 	_DIAGASSERT(fmt != NULL);
622fe8fb19SBen Gras 
632fe8fb19SBen Gras 	_FILEEXT_SETUP(&f, &fext);
642fe8fb19SBen Gras 	f._file = -1;
652fe8fb19SBen Gras 	f._flags = __SWR | __SSTR | __SALC;
66*84d9c625SLionel Sambuc 	f._bf._base = f._p = malloc(128);
672fe8fb19SBen Gras 	if (f._bf._base == NULL)
682fe8fb19SBen Gras 		goto err;
692fe8fb19SBen Gras 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
70*84d9c625SLionel Sambuc 	ret = __vfprintf_unlocked_l(&f, loc, fmt, ap);
712fe8fb19SBen Gras 	if (ret == -1)
722fe8fb19SBen Gras 		goto err;
732fe8fb19SBen Gras 	*f._p = '\0';
742fe8fb19SBen Gras 	_base = realloc(f._bf._base, (size_t)(ret + 1));
752fe8fb19SBen Gras 	if (_base == NULL)
762fe8fb19SBen Gras 		goto err;
772fe8fb19SBen Gras 	*str = (char *)_base;
78f14fb602SLionel Sambuc 	return ret;
792fe8fb19SBen Gras 
802fe8fb19SBen Gras err:
812fe8fb19SBen Gras 	if (f._bf._base)
822fe8fb19SBen Gras 		free(f._bf._base);
832fe8fb19SBen Gras 	*str = NULL;
842fe8fb19SBen Gras 	errno = ENOMEM;
85f14fb602SLionel Sambuc 	return -1;
862fe8fb19SBen Gras }
87*84d9c625SLionel Sambuc 
88*84d9c625SLionel Sambuc int
vasprintf(char ** str,const char * fmt,va_list ap)89*84d9c625SLionel Sambuc vasprintf(char **str, const char *fmt, va_list ap)
90*84d9c625SLionel Sambuc {
91*84d9c625SLionel Sambuc 	return vasprintf_l(str, _current_locale(), fmt, ap);
92*84d9c625SLionel Sambuc }
93*84d9c625SLionel Sambuc 
94*84d9c625SLionel Sambuc int
asprintf_l(char ** str,locale_t loc,char const * fmt,...)95*84d9c625SLionel Sambuc asprintf_l(char **str, locale_t loc, char const *fmt, ...)
96*84d9c625SLionel Sambuc {
97*84d9c625SLionel Sambuc 	va_list ap;
98*84d9c625SLionel Sambuc 	int ret;
99*84d9c625SLionel Sambuc 
100*84d9c625SLionel Sambuc 	va_start(ap, fmt);
101*84d9c625SLionel Sambuc 	ret = vasprintf_l(str, loc, fmt, ap);
102*84d9c625SLionel Sambuc 	va_end(ap);
103*84d9c625SLionel Sambuc 	return ret;
104*84d9c625SLionel Sambuc }
105*84d9c625SLionel Sambuc 
106*84d9c625SLionel Sambuc int
asprintf(char ** str,char const * fmt,...)107*84d9c625SLionel Sambuc asprintf(char **str, char const *fmt, ...)
108*84d9c625SLionel Sambuc {
109*84d9c625SLionel Sambuc 	va_list ap;
110*84d9c625SLionel Sambuc 	int ret;
111*84d9c625SLionel Sambuc 
112*84d9c625SLionel Sambuc 	va_start(ap, fmt);
113*84d9c625SLionel Sambuc 	ret = vasprintf(str, fmt, ap);
114*84d9c625SLionel Sambuc 	va_end(ap);
115*84d9c625SLionel Sambuc 	return ret;
116*84d9c625SLionel Sambuc }
117