vasprintf.c (e7b6782c39ecc02fbfc5545df7fdc0a43c82297e) vasprintf.c (64a965e7078acf9079cbe7f2c001852d4f6f2832)
1/*-
2 * Copyright (c) 1996 Peter Wemm <peter@freebsd.org>
1/* $OpenBSD: vasprintf.c,v 1.4 1998/06/21 22:13:47 millert Exp $ */
2
3/*
4 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
5 * All rights reserved.
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.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
12 *
17 *
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.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#if defined(LIBC_RCS) && !defined(lint)
28 */
29
30#if defined(LIBC_RCS) && !defined(lint)
27static char rcsid[] = "$Id: vasprintf.c,v 1.7 1997/07/06 08:42:37 peter Exp $";
31static char rcsid[] = "$Id: vasprintf.c,v 1.8 1998/03/09 06:51:23 jb Exp $";
28#endif /* LIBC_RCS and not lint */
29
30#include <stdio.h>
31#include <stdlib.h>
32#endif /* LIBC_RCS and not lint */
33
34#include <stdio.h>
35#include <stdlib.h>
32#include <string.h>
36#include <errno.h>
33
37
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;
38int
39vasprintf(str, fmt, ap)
40 char **str;
41 const char *fmt;
89 va_list ap;
42 _BSD_VA_LIST_ ap;
90{
91 int ret;
43{
44 int ret;
92 FILE *f;
93 struct bufcookie h;
45 FILE f;
94
46
95 h.base = malloc(CHUNK_SPARE);
96 if (h.base == NULL)
47 f._file = -1;
48 f._flags = __SWR | __SSTR | __SALC;
49 f._bf._base = f._p = (unsigned char *)malloc(128);
50 if (f._bf._base == NULL) {
51 *str = NULL;
52 errno = ENOMEM;
97 return (-1);
53 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 }
54 }
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);
55 f._bf._size = f._w = 127; /* Leave room for the NULL */
56 ret = vfprintf(&f, fmt, ap);
57 *f._p = '\0';
58 f._bf._base = realloc(f._bf._base, f._bf._size + 1);
59 if (f._bf._base == NULL) {
60 errno = ENOMEM;
61 ret = -1;
118 }
62 }
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';
63 *str = (char *)f._bf._base;
142 return (ret);
143}
64 return (ret);
65}