Blame


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