blob: 32f12150fc4f4a82aace1ff42afc17969f8692a3 [file] [log] [blame]
Sam Ravnborg96e3e182007-07-31 00:38:13 -07001/*
2 * linux/lib/kasprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7#include <stdarg.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05008#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
Sam Ravnborg96e3e182007-07-31 00:38:13 -070010#include <linux/types.h>
11#include <linux/string.h>
12
13/* Simplified asprintf. */
14char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
15{
16 unsigned int len;
17 char *p;
18 va_list aq;
19
20 va_copy(aq, ap);
21 len = vsnprintf(NULL, 0, fmt, aq);
22 va_end(aq);
23
Ezequiel Garcia3e1aa662012-10-10 15:54:04 -070024 p = kmalloc_track_caller(len+1, gfp);
Sam Ravnborg96e3e182007-07-31 00:38:13 -070025 if (!p)
26 return NULL;
27
28 vsnprintf(p, len+1, fmt, ap);
29
30 return p;
31}
32EXPORT_SYMBOL(kvasprintf);
33
34char *kasprintf(gfp_t gfp, const char *fmt, ...)
35{
36 va_list ap;
37 char *p;
38
39 va_start(ap, fmt);
40 p = kvasprintf(gfp, fmt, ap);
41 va_end(ap);
42
43 return p;
44}
45EXPORT_SYMBOL(kasprintf);