lkml.org 
[lkml]   [2006]   [Jun]   [20]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: [PATCH] Implement kasprintf
Randy.Dunlap wrote:
> Doesn't this already add the bloat/code? ::
>
Yup. My back-of-the-mind unexamined assumption was that this was going
into a .a, which it isn't.

Updated patch below.

J


--

From: Jeremy Fitzhardinge <jeremy@xensource.com>

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: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>

---
include/linux/kernel.h | 2 ++
lib/vsprintf.c | 23 +++++++++++++++++++++++
2 files changed, 25 insertions(+)


diff -r cd45ea4bb813 include/linux/kernel.h
--- a/include/linux/kernel.h Tue Jun 20 17:28:16 2006 -0700
+++ b/include/linux/kernel.h Tue Jun 20 17:29:18 2006 -0700
@@ -114,6 +114,8 @@ extern int scnprintf(char * buf, size_t
__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)));
diff -r cd45ea4bb813 lib/vsprintf.c
--- a/lib/vsprintf.c Tue Jun 20 17:28:16 2006 -0700
+++ b/lib/vsprintf.c Tue Jun 20 17:29:18 2006 -0700
@@ -849,3 +849,26 @@ int sscanf(const char * buf, const char
}

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);

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2006-06-21 02:34    [W:0.042 / U:4.720 seconds]
©2003-2017 Jasper Spaans. hosted at Digital OceanAdvertise on this site