xref: /freebsd/lib/libc/stdio/vasprintf.c (revision 3383deca89b096faaebf065ba1a4be55ea1b46f4)
13383decaSTim J. Robbins /*	$OpenBSD: vasprintf.c,v 1.6 1998/10/16 16:11:56 millert Exp $	*/
264a965e7SPeter Wemm 
364a965e7SPeter Wemm /*
464a965e7SPeter Wemm  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
564a965e7SPeter Wemm  * All rights reserved.
615aa00d5SPeter Wemm  *
715aa00d5SPeter Wemm  * Redistribution and use in source and binary forms, with or without
815aa00d5SPeter Wemm  * modification, are permitted provided that the following conditions
915aa00d5SPeter Wemm  * are met:
1015aa00d5SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
1115aa00d5SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
1215aa00d5SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
1315aa00d5SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
1415aa00d5SPeter Wemm  *    documentation and/or other materials provided with the distribution.
1564a965e7SPeter Wemm  * 3. The name of the author may not be used to endorse or promote products
1664a965e7SPeter Wemm  *    derived from this software without specific prior written permission.
1715aa00d5SPeter Wemm  *
1864a965e7SPeter Wemm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
1964a965e7SPeter Wemm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2064a965e7SPeter Wemm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
2164a965e7SPeter Wemm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2264a965e7SPeter Wemm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2364a965e7SPeter Wemm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2464a965e7SPeter Wemm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2564a965e7SPeter Wemm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2664a965e7SPeter Wemm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2764a965e7SPeter Wemm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2815aa00d5SPeter Wemm  */
2915aa00d5SPeter Wemm 
30333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
31333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3215aa00d5SPeter Wemm 
3315aa00d5SPeter Wemm #include <stdio.h>
3415aa00d5SPeter Wemm #include <stdlib.h>
3564a965e7SPeter Wemm #include <errno.h>
36d201fe46SDaniel Eischen #include "local.h"
3715aa00d5SPeter Wemm 
3815aa00d5SPeter Wemm int
3915aa00d5SPeter Wemm vasprintf(str, fmt, ap)
4015aa00d5SPeter Wemm 	char **str;
4115aa00d5SPeter Wemm 	const char *fmt;
42abbd8902SMike Barcroft 	__va_list ap;
4315aa00d5SPeter Wemm {
4415aa00d5SPeter Wemm 	int ret;
4564a965e7SPeter Wemm 	FILE f;
46e74101e4STim J. Robbins 	struct __sFILEX ext;
473383decaSTim J. Robbins 	unsigned char *_base;
4815aa00d5SPeter Wemm 
4964a965e7SPeter Wemm 	f._file = -1;
5064a965e7SPeter Wemm 	f._flags = __SWR | __SSTR | __SALC;
5164a965e7SPeter Wemm 	f._bf._base = f._p = (unsigned char *)malloc(128);
523383decaSTim J. Robbins 	if (f._bf._base == NULL)
533383decaSTim J. Robbins 		goto err;
546879bea8SJuli Mallett 	f._bf._size = f._w = 127;		/* Leave room for the NUL */
55e74101e4STim J. Robbins 	f._extra = &ext;
56e74101e4STim J. Robbins 	INITEXTRA(&f);
57d201fe46SDaniel Eischen 	ret = __vfprintf(&f, fmt, ap);
583383decaSTim J. Robbins 	if (ret == -1)
593383decaSTim J. Robbins 		goto err;
6064a965e7SPeter Wemm 	*f._p = '\0';
613383decaSTim J. Robbins 	_base = realloc(f._bf._base, ret + 1);
623383decaSTim J. Robbins 	if (_base == NULL)
633383decaSTim J. Robbins 		goto err;
643383decaSTim J. Robbins 	*str = (char *)_base;
6515aa00d5SPeter Wemm 	return (ret);
663383decaSTim J. Robbins 
673383decaSTim J. Robbins err:
683383decaSTim J. Robbins 	if (f._bf._base != NULL) {
693383decaSTim J. Robbins 		free(f._bf._base);
703383decaSTim J. Robbins 		f._bf._base = NULL;
713383decaSTim J. Robbins 	}
723383decaSTim J. Robbins 	*str = NULL;
733383decaSTim J. Robbins 	errno = ENOMEM;
743383decaSTim J. Robbins 	return (-1);
7515aa00d5SPeter Wemm }
76