Blame


1 12042ad7 2021-01-21 op /* from mandoc, with tweaks */
2 12042ad7 2021-01-21 op /*
3 12042ad7 2021-01-21 op * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
4 12042ad7 2021-01-21 op *
5 12042ad7 2021-01-21 op * Permission to use, copy, modify, and distribute this software for any
6 12042ad7 2021-01-21 op * purpose with or without fee is hereby granted, provided that the above
7 12042ad7 2021-01-21 op * copyright notice and this permission notice appear in all copies.
8 12042ad7 2021-01-21 op *
9 12042ad7 2021-01-21 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 12042ad7 2021-01-21 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 12042ad7 2021-01-21 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 12042ad7 2021-01-21 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 12042ad7 2021-01-21 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 12042ad7 2021-01-21 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 12042ad7 2021-01-21 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 12042ad7 2021-01-21 op *
17 12042ad7 2021-01-21 op * This fallback implementation is not efficient:
18 12042ad7 2021-01-21 op * It does the formatting twice.
19 12042ad7 2021-01-21 op * Short of fiddling with the unknown internals of the system's
20 12042ad7 2021-01-21 op * printf(3) or completely reimplementing printf(3), i can't think
21 12042ad7 2021-01-21 op * of another portable solution.
22 12042ad7 2021-01-21 op */
23 12042ad7 2021-01-21 op
24 76898714 2021-02-12 op #include "../config.h"
25 76898714 2021-02-12 op
26 12042ad7 2021-01-21 op #include <stdarg.h>
27 12042ad7 2021-01-21 op #include <stdio.h>
28 12042ad7 2021-01-21 op #include <stdlib.h>
29 12042ad7 2021-01-21 op
30 12042ad7 2021-01-21 op int
31 12042ad7 2021-01-21 op vasprintf(char **ret, const char *format, va_list ap)
32 12042ad7 2021-01-21 op {
33 12042ad7 2021-01-21 op char buf[2];
34 12042ad7 2021-01-21 op va_list ap2;
35 12042ad7 2021-01-21 op int sz;
36 12042ad7 2021-01-21 op
37 12042ad7 2021-01-21 op va_copy(ap2, ap);
38 12042ad7 2021-01-21 op sz = vsnprintf(buf, sizeof(buf), format, ap2);
39 12042ad7 2021-01-21 op va_end(ap2);
40 12042ad7 2021-01-21 op
41 12042ad7 2021-01-21 op if (sz != -1 && (*ret = malloc(sz + 1)) != NULL) {
42 12042ad7 2021-01-21 op if (vsnprintf(*ret, sz + 1, format, ap) == sz)
43 12042ad7 2021-01-21 op return sz;
44 12042ad7 2021-01-21 op free(*ret);
45 12042ad7 2021-01-21 op }
46 12042ad7 2021-01-21 op *ret = NULL;
47 12042ad7 2021-01-21 op return -1;
48 12042ad7 2021-01-21 op }