vasprintf.c revision 34380
1/*-
2 * Copyright (c) 1996  Peter Wemm <peter@freebsd.org>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#if defined(LIBC_RCS) && !defined(lint)
27static char rcsid[] = "$Id: vasprintf.c,v 1.7 1997/07/06 08:42:37 peter Exp $";
28#endif /* LIBC_RCS and not lint */
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33
34#if __STDC__
35#include <stdarg.h>
36#else
37#include <varargs.h>
38#endif
39
40#define CHUNK_SPARE 128 /* how much spare to allocate to avoid realloc calls */
41
42struct bufcookie {
43	char	*base;	/* start of buffer */
44	size_t	size;
45	size_t	left;
46};
47
48static int 	writehook __P((void *cookie, const char *, int));
49
50static int
51writehook(cookie, buf, len)
52	void *cookie;
53	const char *buf;
54	int   len;
55{
56	struct bufcookie *h = (struct bufcookie *)cookie;
57	char *newbuf;
58
59	if (len == 0)
60		return 0;
61
62	if (len > h->left) {
63		/* grow malloc region */
64 		/*
65		 * XXX this is linearly expanded, which is slow for obscenely
66		 * large strings.
67		 */
68		h->left = h->left + len + CHUNK_SPARE;
69		h->size = h->size + len + CHUNK_SPARE;
70		newbuf = realloc(h->base, h->size);
71		if (newbuf == NULL) {
72			free(h->base);
73			h->base = NULL;
74			return (-1);
75		} else
76			h->base = newbuf;
77	}
78	/* "write" it */
79	(void)memcpy(h->base + h->size - h->left, buf, (size_t)len);
80	h->left -= len;
81	return (len);
82}
83
84
85int
86vasprintf(str, fmt, ap)
87	char **str;
88	const char *fmt;
89	va_list ap;
90{
91	int ret;
92	FILE *f;
93	struct bufcookie h;
94
95	h.base = malloc(CHUNK_SPARE);
96	if (h.base == NULL)
97		return (-1);
98	h.size = CHUNK_SPARE;
99	h.left = CHUNK_SPARE;
100
101	f = funopen(&h, NULL, writehook, NULL, NULL);
102	if (f == NULL) {
103		free(h.base);
104		return (-1);
105	}
106	ret = vfprintf(f, fmt, ap);
107	fclose(f);
108
109	/*
110	 * clean up the wreckage. Did writehook fail or did something else
111	 * in stdio explode perhaps?
112	 */
113	if (h.base == NULL)	/* realloc failed in writehook */
114		return (-1);
115	if (ret < 0) {		/* something else? */
116		free(h.base);
117		return (-1);
118	}
119
120	/*
121	 * At this point, we have a non-null terminated string in a
122	 * buffer.  There may not be enough room to null-terminate it
123	 * (h.left == 0) - if realloc failes to expand it, it's fatal.
124	 * If we were merely trying to shrink the buffer, a realloc failure
125	 * is not [yet] fatal. Note that when realloc returns NULL,
126	 * the original buffer is left allocated and valid.
127	 */
128	if (h.left == 1)	/* exact fit, do not realloc */
129		*str = h.base;
130	else {
131		*str = realloc(h.base, (size_t)(h.size - h.left + 1));
132		if (*str == NULL) {
133			/* failed to expand? - fatal */
134			if (h.left == 0) {
135				free(h.base);
136				return (-1);
137			}
138			*str = h.base;	/* use oversize original buffer */
139		}
140	}
141	(*str)[h.size - h.left] = '\0';
142	return (ret);
143}
144