2 * linux/lib/kasprintf.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/export.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <linux/string.h>
13 /* Simplified asprintf. */
14 char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
21 len = vsnprintf(NULL, 0, fmt, aq);
24 p = kmalloc_track_caller(len+1, gfp);
28 vsnprintf(p, len+1, fmt, ap);
32 EXPORT_SYMBOL(kvasprintf);
35 * If fmt contains no % (or is exactly %s), use kstrdup_const. If fmt
36 * (or the sole vararg) points to rodata, we will then save a memory
37 * allocation and string copy. In any case, the return value should be
38 * freed using kfree_const().
40 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list ap)
42 if (!strchr(fmt, '%'))
43 return kstrdup_const(fmt, gfp);
44 if (!strcmp(fmt, "%s"))
45 return kstrdup_const(va_arg(ap, const char*), gfp);
46 return kvasprintf(gfp, fmt, ap);
48 EXPORT_SYMBOL(kvasprintf_const);
50 char *kasprintf(gfp_t gfp, const char *fmt, ...)
56 p = kvasprintf(gfp, fmt, ap);
61 EXPORT_SYMBOL(kasprintf);