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