1*f06dce2cSAndrew Stormont /*
2*f06dce2cSAndrew Stormont * This file and its contents are supplied under the terms of the
3*f06dce2cSAndrew Stormont * Common Development and Distribution License ("CDDL"), version 1.0.
4*f06dce2cSAndrew Stormont * You may only use this file in accordance with the terms of version
5*f06dce2cSAndrew Stormont * 1.0 of the CDDL.
6*f06dce2cSAndrew Stormont *
7*f06dce2cSAndrew Stormont * A full copy of the text of the CDDL should have accompanied this
8*f06dce2cSAndrew Stormont * source. A copy of the CDDL is also available via the Internet at
9*f06dce2cSAndrew Stormont * http://www.illumos.org/license/CDDL.
10*f06dce2cSAndrew Stormont */
11*f06dce2cSAndrew Stormont
12*f06dce2cSAndrew Stormont /*
13*f06dce2cSAndrew Stormont * Copyright 2017 RackTop Systems.
14*f06dce2cSAndrew Stormont */
15*f06dce2cSAndrew Stormont
16*f06dce2cSAndrew Stormont #include <sys/kmem.h>
17*f06dce2cSAndrew Stormont #include <sys/varargs.h>
18*f06dce2cSAndrew Stormont #include <sys/cmn_err.h>
19*f06dce2cSAndrew Stormont
20*f06dce2cSAndrew Stormont /*
21*f06dce2cSAndrew Stormont * Do not change the length of the returned string; it must be freed
22*f06dce2cSAndrew Stormont * with strfree().
23*f06dce2cSAndrew Stormont */
24*f06dce2cSAndrew Stormont char *
kmem_asprintf(const char * fmt,...)25*f06dce2cSAndrew Stormont kmem_asprintf(const char *fmt, ...)
26*f06dce2cSAndrew Stormont {
27*f06dce2cSAndrew Stormont int size;
28*f06dce2cSAndrew Stormont va_list adx;
29*f06dce2cSAndrew Stormont char *buf;
30*f06dce2cSAndrew Stormont
31*f06dce2cSAndrew Stormont va_start(adx, fmt);
32*f06dce2cSAndrew Stormont size = vsnprintf(NULL, 0, fmt, adx) + 1;
33*f06dce2cSAndrew Stormont va_end(adx);
34*f06dce2cSAndrew Stormont
35*f06dce2cSAndrew Stormont buf = kmem_alloc(size, KM_SLEEP);
36*f06dce2cSAndrew Stormont
37*f06dce2cSAndrew Stormont va_start(adx, fmt);
38*f06dce2cSAndrew Stormont size = vsnprintf(buf, size, fmt, adx);
39*f06dce2cSAndrew Stormont va_end(adx);
40*f06dce2cSAndrew Stormont
41*f06dce2cSAndrew Stormont return (buf);
42*f06dce2cSAndrew Stormont }
43