From: Jeremy Fitzhardinge <jeremy@goop.org>
To: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Cc: Andrew Morton <akpm@osdl.org>,
Christian Limpach <Christian.Limpach@cl.cam.ac.uk>,
Chris Wright <chrisw@sous-sol.org>
Subject: [PATCH] Implement kasprintf
Date: Tue, 20 Jun 2006 16:57:16 -0700 [thread overview]
Message-ID: <44988B5C.9080400@goop.org> (raw)
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.
Requires vsnprintf to accept a NULL buffer when the buffer size is 0.
Signed-off-by: Jeremy Fitzhardinge <jeremy@xensource.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
---
include/linux/kernel.h | 2 +
lib/Makefile | 2 -
lib/kasprintf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 1 deletion(-)
diff -r c175fd50e604 include/linux/kernel.h
--- a/include/linux/kernel.h Tue Jun 20 16:47:53 2006 -0700
+++ b/include/linux/kernel.h Tue Jun 20 16:53:19 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 c175fd50e604 lib/Makefile
--- a/lib/Makefile Tue Jun 20 16:47:53 2006 -0700
+++ b/lib/Makefile Tue Jun 20 16:53:19 2006 -0700
@@ -5,7 +5,7 @@ lib-y := errno.o ctype.o string.o vsprin
lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \
bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \
idr.o div64.o int_sqrt.o bitmap.o extable.o prio_tree.o \
- sha1.o
+ sha1.o kasprintf.o
lib-$(CONFIG_SMP) += cpumask.o
diff -r c175fd50e604 lib/kasprintf.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/kasprintf.c Tue Jun 20 16:53:19 2006 -0700
@@ -0,0 +1,54 @@
+/******************************************************************************
+ * Simplified asprintf.
+ *
+ * Copyright (C) 2006 XenSource Ltd
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation; or, when distributed
+ * separately from the Linux kernel or incorporated into other
+ * software packages, subject to the following license:
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this source file (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy, modify,
+ * merge, publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+/* 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);
next reply other threads:[~2006-06-20 23:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-06-20 23:57 Jeremy Fitzhardinge [this message]
2006-06-21 0:10 ` [PATCH] Implement kasprintf Randy.Dunlap
2006-06-21 0:14 ` Jeremy Fitzhardinge
2006-06-21 0:26 ` Randy.Dunlap
2006-06-21 0:31 ` Jeremy Fitzhardinge
2006-06-21 3:04 ` Kyle McMartin
2006-06-21 3:26 ` Andrew Morton
2006-06-21 4:30 ` H. Peter Anvin
2006-06-21 11:58 ` Kyle McMartin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=44988B5C.9080400@goop.org \
--to=jeremy@goop.org \
--cc=Christian.Limpach@cl.cam.ac.uk \
--cc=akpm@osdl.org \
--cc=chrisw@sous-sol.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox