Blame


1 dd038bc6 2021-09-21 thomas.ad /*
2 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2006 Nicholas Marriott <nicholas.marriott@gmail.com>
3 dd038bc6 2021-09-21 thomas.ad *
4 dd038bc6 2021-09-21 thomas.ad * Permission to use, copy, modify, and distribute this software for any
5 dd038bc6 2021-09-21 thomas.ad * purpose with or without fee is hereby granted, provided that the above
6 dd038bc6 2021-09-21 thomas.ad * copyright notice and this permission notice appear in all copies.
7 dd038bc6 2021-09-21 thomas.ad *
8 dd038bc6 2021-09-21 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 dd038bc6 2021-09-21 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 dd038bc6 2021-09-21 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 dd038bc6 2021-09-21 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 dd038bc6 2021-09-21 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
13 dd038bc6 2021-09-21 thomas.ad * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
14 dd038bc6 2021-09-21 thomas.ad * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 dd038bc6 2021-09-21 thomas.ad */
16 dd038bc6 2021-09-21 thomas.ad
17 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
18 dd038bc6 2021-09-21 thomas.ad
19 dd038bc6 2021-09-21 thomas.ad #include <err.h>
20 dd038bc6 2021-09-21 thomas.ad #include <stdarg.h>
21 dd038bc6 2021-09-21 thomas.ad #include <stdio.h>
22 dd038bc6 2021-09-21 thomas.ad #include <string.h>
23 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
24 dd038bc6 2021-09-21 thomas.ad
25 4fccd2fe 2023-03-08 thomas int vasprintf(char **, const char *, va_list);
26 4fccd2fe 2023-03-08 thomas
27 dd038bc6 2021-09-21 thomas.ad int
28 dd038bc6 2021-09-21 thomas.ad asprintf(char **ret, const char *fmt, ...)
29 dd038bc6 2021-09-21 thomas.ad {
30 dd038bc6 2021-09-21 thomas.ad va_list ap;
31 dd038bc6 2021-09-21 thomas.ad int n;
32 dd038bc6 2021-09-21 thomas.ad
33 dd038bc6 2021-09-21 thomas.ad va_start(ap, fmt);
34 dd038bc6 2021-09-21 thomas.ad n = vasprintf(ret, fmt, ap);
35 dd038bc6 2021-09-21 thomas.ad va_end(ap);
36 dd038bc6 2021-09-21 thomas.ad
37 dd038bc6 2021-09-21 thomas.ad return (n);
38 dd038bc6 2021-09-21 thomas.ad }
39 dd038bc6 2021-09-21 thomas.ad
40 dd038bc6 2021-09-21 thomas.ad int
41 dd038bc6 2021-09-21 thomas.ad vasprintf(char **ret, const char *fmt, va_list ap)
42 dd038bc6 2021-09-21 thomas.ad {
43 dd038bc6 2021-09-21 thomas.ad int n;
44 dd038bc6 2021-09-21 thomas.ad va_list ap2;
45 dd038bc6 2021-09-21 thomas.ad
46 dd038bc6 2021-09-21 thomas.ad va_copy(ap2, ap);
47 dd038bc6 2021-09-21 thomas.ad
48 dd038bc6 2021-09-21 thomas.ad if ((n = vsnprintf(NULL, 0, fmt, ap)) < 0)
49 dd038bc6 2021-09-21 thomas.ad goto error;
50 dd038bc6 2021-09-21 thomas.ad
51 dd038bc6 2021-09-21 thomas.ad *ret = malloc(n + 1);
52 dd038bc6 2021-09-21 thomas.ad if (*ret == NULL)
53 dd038bc6 2021-09-21 thomas.ad errx(1, "vasprintf: malloc failed");
54 dd038bc6 2021-09-21 thomas.ad if ((n = vsnprintf(*ret, n + 1, fmt, ap2)) < 0) {
55 dd038bc6 2021-09-21 thomas.ad free(*ret);
56 dd038bc6 2021-09-21 thomas.ad goto error;
57 dd038bc6 2021-09-21 thomas.ad }
58 dd038bc6 2021-09-21 thomas.ad va_end(ap2);
59 dd038bc6 2021-09-21 thomas.ad
60 dd038bc6 2021-09-21 thomas.ad return (n);
61 dd038bc6 2021-09-21 thomas.ad
62 dd038bc6 2021-09-21 thomas.ad error:
63 dd038bc6 2021-09-21 thomas.ad va_end(ap2);
64 dd038bc6 2021-09-21 thomas.ad *ret = NULL;
65 dd038bc6 2021-09-21 thomas.ad return (-1);
66 dd038bc6 2021-09-21 thomas.ad }