1*5087Sjp161948 /*
2*5087Sjp161948  * Copyright (c) 2004 Darren Tucker.
3*5087Sjp161948  *
4*5087Sjp161948  * Based originally on asprintf.c from OpenBSD:
5*5087Sjp161948  * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
6*5087Sjp161948  *
7*5087Sjp161948  * Permission to use, copy, modify, and distribute this software for any
8*5087Sjp161948  * purpose with or without fee is hereby granted, provided that the above
9*5087Sjp161948  * copyright notice and this permission notice appear in all copies.
10*5087Sjp161948  *
11*5087Sjp161948  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12*5087Sjp161948  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*5087Sjp161948  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14*5087Sjp161948  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*5087Sjp161948  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*5087Sjp161948  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17*5087Sjp161948  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*5087Sjp161948  */
19*5087Sjp161948 
20*5087Sjp161948 #pragma ident	"%Z%%M%	%I%	%E% SMI"
21*5087Sjp161948 
22*5087Sjp161948 #include "includes.h"
23*5087Sjp161948 
24*5087Sjp161948 #ifndef HAVE_VASPRINTF
25*5087Sjp161948 
26*5087Sjp161948 #include <errno.h>
27*5087Sjp161948 #include <stdarg.h>
28*5087Sjp161948 #include <stdlib.h>
29*5087Sjp161948 
30*5087Sjp161948 #ifndef VA_COPY
31*5087Sjp161948 #ifdef HAVE_VA_COPY
32*5087Sjp161948 #define	VA_COPY(dest, src) va_copy(dest, src)
33*5087Sjp161948 #else
34*5087Sjp161948 #ifdef HAVE___VA_COPY
35*5087Sjp161948 #define	VA_COPY(dest, src) __va_copy(dest, src)
36*5087Sjp161948 #else
37*5087Sjp161948 #define	VA_COPY(dest, src) (dest) = (src)
38*5087Sjp161948 #endif
39*5087Sjp161948 #endif
40*5087Sjp161948 #endif
41*5087Sjp161948 
42*5087Sjp161948 #define	INIT_SZ	128
43*5087Sjp161948 
44*5087Sjp161948 int
vasprintf(char ** str,const char * fmt,va_list ap)45*5087Sjp161948 vasprintf(char **str, const char *fmt, va_list ap)
46*5087Sjp161948 {
47*5087Sjp161948 	int ret = -1;
48*5087Sjp161948 	va_list ap2;
49*5087Sjp161948 	char *string, *newstr;
50*5087Sjp161948 	size_t len;
51*5087Sjp161948 
52*5087Sjp161948 	VA_COPY(ap2, ap);
53*5087Sjp161948 	if ((string = malloc(INIT_SZ)) == NULL)
54*5087Sjp161948 		goto fail;
55*5087Sjp161948 
56*5087Sjp161948 	ret = vsnprintf(string, INIT_SZ, fmt, ap2);
57*5087Sjp161948 	if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
58*5087Sjp161948 		*str = string;
59*5087Sjp161948 	} else if (ret == INT_MAX || ret < 0) { /* Bad length */
60*5087Sjp161948 		free(string);
61*5087Sjp161948 		goto fail;
62*5087Sjp161948 	} else {	/* bigger than initial, realloc allowing for nul */
63*5087Sjp161948 		len = (size_t)ret + 1;
64*5087Sjp161948 		if ((newstr = realloc(string, len)) == NULL) {
65*5087Sjp161948 			free(string);
66*5087Sjp161948 			goto fail;
67*5087Sjp161948 		} else {
68*5087Sjp161948 			va_end(ap2);
69*5087Sjp161948 			VA_COPY(ap2, ap);
70*5087Sjp161948 			ret = vsnprintf(newstr, len, fmt, ap2);
71*5087Sjp161948 			if (ret >= 0 && (size_t)ret < len) {
72*5087Sjp161948 				*str = newstr;
73*5087Sjp161948 			} else { /* failed with realloc'ed string, give up */
74*5087Sjp161948 				free(newstr);
75*5087Sjp161948 				goto fail;
76*5087Sjp161948 			}
77*5087Sjp161948 		}
78*5087Sjp161948 	}
79*5087Sjp161948 	va_end(ap2);
80*5087Sjp161948 	return (ret);
81*5087Sjp161948 
82*5087Sjp161948 fail:
83*5087Sjp161948 	*str = NULL;
84*5087Sjp161948 	errno = ENOMEM;
85*5087Sjp161948 	va_end(ap2);
86*5087Sjp161948 	return (-1);
87*5087Sjp161948 }
88*5087Sjp161948 #endif
89*5087Sjp161948 
90*5087Sjp161948 #ifndef HAVE_ASPRINTF
91*5087Sjp161948 int
asprintf(char ** str,const char * fmt,...)92*5087Sjp161948 asprintf(char **str, const char *fmt, ...)
93*5087Sjp161948 {
94*5087Sjp161948 	va_list ap;
95*5087Sjp161948 	int ret;
96*5087Sjp161948 
97*5087Sjp161948 	*str = NULL;
98*5087Sjp161948 	va_start(ap, fmt);
99*5087Sjp161948 	ret = vasprintf(str, fmt, ap);
100*5087Sjp161948 	va_end(ap);
101*5087Sjp161948 
102*5087Sjp161948 	return (ret);
103*5087Sjp161948 }
104*5087Sjp161948 #endif
105