Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Sam Ravnborg | 96e3e18 | 2007-07-31 00:38:13 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/lib/kasprintf.c |
| 4 | * |
| 5 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 6 | */ |
| 7 | |
Alexey Dobriyan | c0891ac | 2021-08-02 23:40:32 +0300 | [diff] [blame] | 8 | #include <linux/stdarg.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Sam Ravnborg | 96e3e18 | 2007-07-31 00:38:13 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/string.h> |
| 13 | |
| 14 | /* Simplified asprintf. */ |
| 15 | char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap) |
| 16 | { |
Rasmus Villemoes | 8e2a2bf | 2016-01-15 16:58:47 -0800 | [diff] [blame] | 17 | unsigned int first, second; |
Sam Ravnborg | 96e3e18 | 2007-07-31 00:38:13 -0700 | [diff] [blame] | 18 | char *p; |
| 19 | va_list aq; |
| 20 | |
| 21 | va_copy(aq, ap); |
Rasmus Villemoes | 8e2a2bf | 2016-01-15 16:58:47 -0800 | [diff] [blame] | 22 | first = vsnprintf(NULL, 0, fmt, aq); |
Sam Ravnborg | 96e3e18 | 2007-07-31 00:38:13 -0700 | [diff] [blame] | 23 | va_end(aq); |
| 24 | |
Rasmus Villemoes | 8e2a2bf | 2016-01-15 16:58:47 -0800 | [diff] [blame] | 25 | p = kmalloc_track_caller(first+1, gfp); |
Sam Ravnborg | 96e3e18 | 2007-07-31 00:38:13 -0700 | [diff] [blame] | 26 | if (!p) |
| 27 | return NULL; |
| 28 | |
Rasmus Villemoes | 8e2a2bf | 2016-01-15 16:58:47 -0800 | [diff] [blame] | 29 | second = vsnprintf(p, first+1, fmt, ap); |
| 30 | WARN(first != second, "different return values (%u and %u) from vsnprintf(\"%s\", ...)", |
| 31 | first, second, fmt); |
Sam Ravnborg | 96e3e18 | 2007-07-31 00:38:13 -0700 | [diff] [blame] | 32 | |
| 33 | return p; |
| 34 | } |
| 35 | EXPORT_SYMBOL(kvasprintf); |
| 36 | |
Rasmus Villemoes | 0a9df78 | 2015-11-06 16:31:20 -0800 | [diff] [blame] | 37 | /* |
| 38 | * If fmt contains no % (or is exactly %s), use kstrdup_const. If fmt |
| 39 | * (or the sole vararg) points to rodata, we will then save a memory |
| 40 | * allocation and string copy. In any case, the return value should be |
| 41 | * freed using kfree_const(). |
| 42 | */ |
| 43 | const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list ap) |
| 44 | { |
| 45 | if (!strchr(fmt, '%')) |
| 46 | return kstrdup_const(fmt, gfp); |
| 47 | if (!strcmp(fmt, "%s")) |
| 48 | return kstrdup_const(va_arg(ap, const char*), gfp); |
| 49 | return kvasprintf(gfp, fmt, ap); |
| 50 | } |
| 51 | EXPORT_SYMBOL(kvasprintf_const); |
| 52 | |
Sam Ravnborg | 96e3e18 | 2007-07-31 00:38:13 -0700 | [diff] [blame] | 53 | char *kasprintf(gfp_t gfp, const char *fmt, ...) |
| 54 | { |
| 55 | va_list ap; |
| 56 | char *p; |
| 57 | |
| 58 | va_start(ap, fmt); |
| 59 | p = kvasprintf(gfp, fmt, ap); |
| 60 | va_end(ap); |
| 61 | |
| 62 | return p; |
| 63 | } |
| 64 | EXPORT_SYMBOL(kasprintf); |