Home | History | Annotate | Line # | Download | only in stdio
vasprintf.c revision 1.1
      1  1.1  perry /*	$NetBSD: vasprintf.c,v 1.1 1998/08/28 21:33:11 perry Exp $	*/
      2  1.1  perry 
      3  1.1  perry /*
      4  1.1  perry  * Copyright (c) 1997 Todd C. Miller <Todd.Miller (at) courtesan.com>
      5  1.1  perry  * All rights reserved.
      6  1.1  perry  *
      7  1.1  perry  * Redistribution and use in source and binary forms, with or without
      8  1.1  perry  * modification, are permitted provided that the following conditions
      9  1.1  perry  * are met:
     10  1.1  perry  * 1. Redistributions of source code must retain the above copyright
     11  1.1  perry  *    notice, this list of conditions and the following disclaimer.
     12  1.1  perry  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  perry  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  perry  *    documentation and/or other materials provided with the distribution.
     15  1.1  perry  * 3. The name of the author may not be used to endorse or promote products
     16  1.1  perry  *    derived from this software without specific prior written permission.
     17  1.1  perry  *
     18  1.1  perry  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     19  1.1  perry  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     20  1.1  perry  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     21  1.1  perry  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22  1.1  perry  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23  1.1  perry  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     24  1.1  perry  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25  1.1  perry  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1  perry  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     27  1.1  perry  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  perry  */
     29  1.1  perry 
     30  1.1  perry #include <sys/cdefs.h>
     31  1.1  perry #if defined(LIBC_SCCS) && !defined(lint)
     32  1.1  perry __RCSID("$NetBSD: vasprintf.c,v 1.1 1998/08/28 21:33:11 perry Exp $");
     33  1.1  perry #endif /* LIBC_SCCS and not lint */
     34  1.1  perry 
     35  1.1  perry #include <stdio.h>
     36  1.1  perry #include <stdlib.h>
     37  1.1  perry #include <errno.h>
     38  1.1  perry 
     39  1.1  perry int
     40  1.1  perry vasprintf(str, fmt, ap)
     41  1.1  perry 	char **str;
     42  1.1  perry 	const char *fmt;
     43  1.1  perry 	_BSD_VA_LIST_ ap;
     44  1.1  perry {
     45  1.1  perry 	int ret;
     46  1.1  perry 	FILE f;
     47  1.1  perry 	unsigned char *_base;
     48  1.1  perry 
     49  1.1  perry 	f._file = -1;
     50  1.1  perry 	f._flags = __SWR | __SSTR | __SALC;
     51  1.1  perry 	f._bf._base = f._p = (unsigned char *)malloc(128);
     52  1.1  perry 	if (f._bf._base == NULL) {
     53  1.1  perry 		*str = NULL;
     54  1.1  perry 		errno = ENOMEM;
     55  1.1  perry 		return (-1);
     56  1.1  perry 	}
     57  1.1  perry 	f._bf._size = f._w = 127;		/* Leave room for the NULL */
     58  1.1  perry 	ret = vfprintf(&f, fmt, ap);
     59  1.1  perry 	*f._p = '\0';
     60  1.1  perry 	_base = realloc(f._bf._base, f._bf._size + 1);
     61  1.1  perry 	if (_base == NULL) {
     62  1.1  perry 		if (f._bf._base)
     63  1.1  perry 			free(f._bf._base);
     64  1.1  perry 		f._bf._base = NULL;
     65  1.1  perry 		errno = ENOMEM;
     66  1.1  perry 		ret = -1;
     67  1.1  perry 	}
     68  1.1  perry 	f._bf._base = _base;
     69  1.1  perry 	*str = (char *)f._bf._base;
     70  1.1  perry 	return (ret);
     71  1.1  perry }
     72