xref: /freebsd/lib/libc/stdio/vasprintf.c (revision 74f1007fcc838501c74a633792c3f01833bf65e1)
127a29543STim J. Robbins /*	$OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $	*/
264a965e7SPeter Wemm 
3d915a14eSPedro F. Giffuni /*-
4d915a14eSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
5d915a14eSPedro F. Giffuni  *
664a965e7SPeter Wemm  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
764a965e7SPeter Wemm  * All rights reserved.
815aa00d5SPeter Wemm  *
93c87aa1dSDavid Chisnall  * Copyright (c) 2011 The FreeBSD Foundation
105b5fa75aSEd Maste  *
113c87aa1dSDavid Chisnall  * Portions of this software were developed by David Chisnall
123c87aa1dSDavid Chisnall  * under sponsorship from the FreeBSD Foundation.
133c87aa1dSDavid Chisnall  *
1415aa00d5SPeter Wemm  * Redistribution and use in source and binary forms, with or without
1515aa00d5SPeter Wemm  * modification, are permitted provided that the following conditions
1615aa00d5SPeter Wemm  * are met:
1715aa00d5SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
1815aa00d5SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
1915aa00d5SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
2015aa00d5SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
2115aa00d5SPeter Wemm  *    documentation and/or other materials provided with the distribution.
2264a965e7SPeter Wemm  * 3. The name of the author may not be used to endorse or promote products
2364a965e7SPeter Wemm  *    derived from this software without specific prior written permission.
2415aa00d5SPeter Wemm  *
2564a965e7SPeter Wemm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2664a965e7SPeter Wemm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2764a965e7SPeter Wemm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2864a965e7SPeter Wemm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2964a965e7SPeter Wemm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3064a965e7SPeter Wemm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3164a965e7SPeter Wemm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3264a965e7SPeter Wemm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3364a965e7SPeter Wemm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3464a965e7SPeter Wemm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3515aa00d5SPeter Wemm  */
3615aa00d5SPeter Wemm 
37*74f1007fSDag-Erling Smørgrav #include <errno.h>
3815aa00d5SPeter Wemm #include <stdio.h>
3915aa00d5SPeter Wemm #include <stdlib.h>
403c87aa1dSDavid Chisnall #include "xlocale_private.h"
41d201fe46SDaniel Eischen #include "local.h"
4215aa00d5SPeter Wemm 
4315aa00d5SPeter Wemm int
vasprintf_l(char ** str,locale_t locale,const char * fmt,__va_list ap)443c87aa1dSDavid Chisnall vasprintf_l(char **str, locale_t locale, const char *fmt, __va_list ap)
4515aa00d5SPeter Wemm {
461b0181dfSJohn Baldwin 	FILE f = FAKE_FILE;
47*74f1007fSDag-Erling Smørgrav 	int serrno = errno;
486a18a772SDavid E. O'Brien 	int ret;
493c87aa1dSDavid Chisnall 	FIX_LOCALE(locale);
5015aa00d5SPeter Wemm 
5164a965e7SPeter Wemm 	f._flags = __SWR | __SSTR | __SALC;
526a18a772SDavid E. O'Brien 	f._bf._base = f._p = malloc(128);
5327a29543STim J. Robbins 	if (f._bf._base == NULL) {
5427a29543STim J. Robbins 		*str = NULL;
5527a29543STim J. Robbins 		errno = ENOMEM;
5627a29543STim J. Robbins 		return (-1);
5727a29543STim J. Robbins 	}
586879bea8SJuli Mallett 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
59*74f1007fSDag-Erling Smørgrav 	ret = __vfprintf(&f, locale, serrno, fmt, ap);
6027a29543STim J. Robbins 	if (ret < 0) {
613383decaSTim J. Robbins 		free(f._bf._base);
623383decaSTim J. Robbins 		*str = NULL;
633383decaSTim J. Robbins 		errno = ENOMEM;
643383decaSTim J. Robbins 		return (-1);
6515aa00d5SPeter Wemm 	}
6627a29543STim J. Robbins 	*f._p = '\0';
6727a29543STim J. Robbins 	*str = (char *)f._bf._base;
6827a29543STim J. Robbins 	return (ret);
6927a29543STim J. Robbins }
703c87aa1dSDavid Chisnall int
vasprintf(char ** str,const char * fmt,__va_list ap)713c87aa1dSDavid Chisnall vasprintf(char **str, const char *fmt, __va_list ap)
723c87aa1dSDavid Chisnall {
733c87aa1dSDavid Chisnall 	return vasprintf_l(str, __get_locale(), fmt, ap);
743c87aa1dSDavid Chisnall }
75