From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/2] stringlist: implement string_list_asprintf
Date: Fri, 10 Jun 2011 08:57:53 +0200 [thread overview]
Message-ID: <1307689073-9559-3-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1307689073-9559-1-git-send-email-s.hauer@pengutronix.de>
Useful for allocating a string list entry on the fly.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
include/stringlist.h | 1 +
lib/stringlist.c | 22 ++++++++++++++++++++++
2 files changed, 23 insertions(+), 0 deletions(-)
diff --git a/include/stringlist.h b/include/stringlist.h
index 86edcf0..1e2616d 100644
--- a/include/stringlist.h
+++ b/include/stringlist.h
@@ -9,6 +9,7 @@ struct string_list {
};
int string_list_add(struct string_list *sl, char *str);
+int string_list_asprintf(struct string_list *sl, const char *fmt, ...);
void string_list_print_by_column(struct string_list *sl);
static inline void string_list_init(struct string_list *sl)
diff --git a/lib/stringlist.c b/lib/stringlist.c
index 3c8ecec..1015cac 100644
--- a/lib/stringlist.c
+++ b/lib/stringlist.c
@@ -1,6 +1,7 @@
#include <common.h>
#include <xfuncs.h>
#include <malloc.h>
+#include <errno.h>
#include <xfuncs.h>
#include <stringlist.h>
@@ -16,6 +17,27 @@ int string_list_add(struct string_list *sl, char *str)
return 0;
}
+int string_list_asprintf(struct string_list *sl, const char *fmt, ...)
+{
+ struct string_list *new;
+ va_list args;
+
+ new = xmalloc(sizeof(*new));
+
+ va_start(args, fmt);
+
+ new->str = asprintf(fmt, args);
+
+ va_end(args);
+
+ if (!new->str) {
+ free(new);
+ return -ENOMEM;
+ }
+
+ return 0;
+}
+
void string_list_print_by_column(struct string_list *sl)
{
int len = 0, num, i;
--
1.7.5.3
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
prev parent reply other threads:[~2011-06-10 6:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-06-10 6:57 [PATCH] improve string_list Sascha Hauer
2011-06-10 6:57 ` [PATCH 1/2] stringlist: use seperately allocated string Sascha Hauer
2011-06-10 6:57 ` Sascha Hauer [this message]
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=1307689073-9559-3-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.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