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 dd038bc6 2021-09-21 thomas.ad int
26 dd038bc6 2021-09-21 thomas.ad asprintf(char **ret, const char *fmt, ...)
27 dd038bc6 2021-09-21 thomas.ad {
28 dd038bc6 2021-09-21 thomas.ad va_list ap;
29 dd038bc6 2021-09-21 thomas.ad int n;
30 dd038bc6 2021-09-21 thomas.ad
31 dd038bc6 2021-09-21 thomas.ad va_start(ap, fmt);
32 dd038bc6 2021-09-21 thomas.ad n = vasprintf(ret, fmt, ap);
33 dd038bc6 2021-09-21 thomas.ad va_end(ap);
34 dd038bc6 2021-09-21 thomas.ad
35 dd038bc6 2021-09-21 thomas.ad return (n);
36 dd038bc6 2021-09-21 thomas.ad }
37 dd038bc6 2021-09-21 thomas.ad
38 dd038bc6 2021-09-21 thomas.ad int
39 dd038bc6 2021-09-21 thomas.ad vasprintf(char **ret, const char *fmt, va_list ap)
40 dd038bc6 2021-09-21 thomas.ad {
41 dd038bc6 2021-09-21 thomas.ad int n;
42 dd038bc6 2021-09-21 thomas.ad va_list ap2;
43 dd038bc6 2021-09-21 thomas.ad
44 dd038bc6 2021-09-21 thomas.ad va_copy(ap2, ap);
45 dd038bc6 2021-09-21 thomas.ad
46 dd038bc6 2021-09-21 thomas.ad if ((n = vsnprintf(NULL, 0, fmt, ap)) < 0)
47 dd038bc6 2021-09-21 thomas.ad goto error;
48 dd038bc6 2021-09-21 thomas.ad
49 dd038bc6 2021-09-21 thomas.ad *ret = malloc(n + 1);
50 dd038bc6 2021-09-21 thomas.ad if (*ret == NULL)
51 dd038bc6 2021-09-21 thomas.ad errx(1, "vasprintf: malloc failed");
52 dd038bc6 2021-09-21 thomas.ad if ((n = vsnprintf(*ret, n + 1, fmt, ap2)) < 0) {
53 dd038bc6 2021-09-21 thomas.ad free(*ret);
54 dd038bc6 2021-09-21 thomas.ad goto error;
55 dd038bc6 2021-09-21 thomas.ad }
56 dd038bc6 2021-09-21 thomas.ad va_end(ap2);
57 dd038bc6 2021-09-21 thomas.ad
58 dd038bc6 2021-09-21 thomas.ad return (n);
59 dd038bc6 2021-09-21 thomas.ad
60 dd038bc6 2021-09-21 thomas.ad error:
61 dd038bc6 2021-09-21 thomas.ad va_end(ap2);
62 dd038bc6 2021-09-21 thomas.ad *ret = NULL;
63 dd038bc6 2021-09-21 thomas.ad return (-1);
64 dd038bc6 2021-09-21 thomas.ad }