Commit e905914f authored by Jeremy Fitzhardinge's avatar Jeremy Fitzhardinge Committed by Linus Torvalds

[PATCH] Implement kasprintf

Implement kasprintf, a kernel version of asprintf.  This allocates the
memory required for the formatted string, including the trailing '\0'.
Returns NULL on allocation failure.
Signed-off-by: default avatarJeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: default avatarChris Wright <chrisw@sous-sol.org>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent f796937a
......@@ -117,6 +117,8 @@ extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
__attribute__ ((format (printf, 3, 4)));
extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
__attribute__ ((format (printf, 3, 0)));
extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
__attribute__ ((format (printf, 2, 3)));
extern int sscanf(const char *, const char *, ...)
__attribute__ ((format (scanf, 2, 3)));
......
......@@ -849,3 +849,26 @@ int sscanf(const char * buf, const char * fmt, ...)
}
EXPORT_SYMBOL(sscanf);
/* Simplified asprintf. */
char *kasprintf(gfp_t gfp, const char *fmt, ...)
{
va_list ap;
unsigned int len;
char *p;
va_start(ap, fmt);
len = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
p = kmalloc(len+1, gfp);
if (!p)
return NULL;
va_start(ap, fmt);
vsnprintf(p, len+1, fmt, ap);
va_end(ap);
return p;
}
EXPORT_SYMBOL(kasprintf);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment