1*3cd45331SRyan Stone /*-
2*3cd45331SRyan Stone * Copyright (c) 2013-2015 Sandvine Inc. All rights reserved.
3*3cd45331SRyan Stone * All rights reserved.
4*3cd45331SRyan Stone *
5*3cd45331SRyan Stone * Redistribution and use in source and binary forms, with or without
6*3cd45331SRyan Stone * modification, are permitted provided that the following conditions
7*3cd45331SRyan Stone * are met:
8*3cd45331SRyan Stone * 1. Redistributions of source code must retain the above copyright
9*3cd45331SRyan Stone * notice, this list of conditions and the following disclaimer.
10*3cd45331SRyan Stone * 2. Redistributions in binary form must reproduce the above copyright
11*3cd45331SRyan Stone * notice, this list of conditions and the following disclaimer in the
12*3cd45331SRyan Stone * documentation and/or other materials provided with the distribution.
13*3cd45331SRyan Stone *
14*3cd45331SRyan Stone * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*3cd45331SRyan Stone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*3cd45331SRyan Stone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*3cd45331SRyan Stone * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*3cd45331SRyan Stone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*3cd45331SRyan Stone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*3cd45331SRyan Stone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*3cd45331SRyan Stone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*3cd45331SRyan Stone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*3cd45331SRyan Stone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*3cd45331SRyan Stone * SUCH DAMAGE.
25*3cd45331SRyan Stone */
26*3cd45331SRyan Stone
27*3cd45331SRyan Stone #include <sys/param.h>
28*3cd45331SRyan Stone #include <sys/systm.h>
29*3cd45331SRyan Stone #include <sys/malloc.h>
30*3cd45331SRyan Stone
31*3cd45331SRyan Stone #include <machine/stdarg.h>
32*3cd45331SRyan Stone
33*3cd45331SRyan Stone int
vasprintf(char ** buf,struct malloc_type * mtp,const char * format,va_list va)34*3cd45331SRyan Stone vasprintf(char **buf, struct malloc_type *mtp, const char *format, va_list va)
35*3cd45331SRyan Stone {
36*3cd45331SRyan Stone int len, ret;
37*3cd45331SRyan Stone va_list tmp_va;
38*3cd45331SRyan Stone char dummy;
39*3cd45331SRyan Stone
40*3cd45331SRyan Stone va_copy(tmp_va, va);
41*3cd45331SRyan Stone len = vsnprintf(&dummy, 0, format, tmp_va);
42*3cd45331SRyan Stone va_end(tmp_va);
43*3cd45331SRyan Stone if (len < 0) {
44*3cd45331SRyan Stone *buf = NULL;
45*3cd45331SRyan Stone return (len);
46*3cd45331SRyan Stone }
47*3cd45331SRyan Stone
48*3cd45331SRyan Stone /* Account for null terminator. */
49*3cd45331SRyan Stone len += 1;
50*3cd45331SRyan Stone *buf = malloc(len, mtp, M_NOWAIT);
51*3cd45331SRyan Stone if (*buf == NULL)
52*3cd45331SRyan Stone return (-1);
53*3cd45331SRyan Stone
54*3cd45331SRyan Stone ret = vsnprintf(*buf, len, format, va);
55*3cd45331SRyan Stone if (ret < 0) {
56*3cd45331SRyan Stone free(*buf, mtp);
57*3cd45331SRyan Stone *buf = NULL;
58*3cd45331SRyan Stone }
59*3cd45331SRyan Stone
60*3cd45331SRyan Stone return (ret);
61*3cd45331SRyan Stone }
62*3cd45331SRyan Stone
63*3cd45331SRyan Stone int
asprintf(char ** buf,struct malloc_type * mtp,const char * format,...)64*3cd45331SRyan Stone asprintf(char **buf, struct malloc_type *mtp, const char *format, ...)
65*3cd45331SRyan Stone {
66*3cd45331SRyan Stone int ret;
67*3cd45331SRyan Stone va_list va;
68*3cd45331SRyan Stone
69*3cd45331SRyan Stone va_start(va, format);
70*3cd45331SRyan Stone ret = vasprintf(buf, mtp, format, va);
71*3cd45331SRyan Stone va_end(va);
72*3cd45331SRyan Stone
73*3cd45331SRyan Stone return (ret);
74*3cd45331SRyan Stone }
75