]>
git.ipfire.org Git - thirdparty/linux.git/blob - lib/kasprintf.c
1 // SPDX-License-Identifier: GPL-2.0
3 * linux/lib/kasprintf.c
5 * Copyright (C) 1991, 1992 Linus Torvalds
8 #include <linux/stdarg.h>
9 #include <linux/export.h>
10 #include <linux/slab.h>
11 #include <linux/types.h>
12 #include <linux/string.h>
14 /* Simplified asprintf. */
15 char *kvasprintf(gfp_t gfp
, const char *fmt
, va_list ap
)
17 unsigned int first
, second
;
22 first
= vsnprintf(NULL
, 0, fmt
, aq
);
25 p
= kmalloc_track_caller(first
+1, gfp
);
29 second
= vsnprintf(p
, first
+1, fmt
, ap
);
30 WARN(first
!= second
, "different return values (%u and %u) from vsnprintf(\"%s\", ...)",
35 EXPORT_SYMBOL(kvasprintf
);
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().
43 const char *kvasprintf_const(gfp_t gfp
, const char *fmt
, va_list ap
)
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
);
51 EXPORT_SYMBOL(kvasprintf_const
);
53 char *kasprintf(gfp_t gfp
, const char *fmt
, ...)
59 p
= kvasprintf(gfp
, fmt
, ap
);
64 EXPORT_SYMBOL(kasprintf
);