diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2012-04-19 10:36:28 +0800 |
---|---|---|
committer | Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com> | 2012-04-30 20:22:23 +0800 |
commit | f152f6a7701b943cfa67c6bfb42a22086861daa3 (patch) | |
tree | 6159e17f9b823c9db7de2fa7b8950af14eabb57f | |
parent | 34faa2e7ca95f8f7f4a235bb5b25fd604612f140 (diff) | |
download | barebox-f152f6a7701b943cfa67c6bfb42a22086861daa3.tar.gz barebox-f152f6a7701b943cfa67c6bfb42a22086861daa3.tar.xz |
stringlist: implement string_list_add_asprintf
Useful for allocating a string list entry on the fly.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
-rw-r--r-- | include/stringlist.h | 1 | ||||
-rw-r--r-- | lib/stringlist.c | 24 |
2 files changed, 25 insertions, 0 deletions
diff --git a/include/stringlist.h b/include/stringlist.h index 4b3cbf3118..dd3f623618 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_add_asprintf(struct string_list *sl, const char *fmt, ...); int string_list_add_sorted(struct string_list *sl, char *str); int string_list_contains(struct string_list *sl, char *str); void string_list_print_by_column(struct string_list *sl); diff --git a/lib/stringlist.c b/lib/stringlist.c index c8b835ed09..b965aa058c 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 <stringlist.h> static int string_list_compare(struct list_head *a, struct list_head *b) @@ -24,6 +25,29 @@ int string_list_add(struct string_list *sl, char *str) return 0; } +int string_list_add_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 = vasprintf(fmt, args); + + va_end(args); + + if (!new->str) { + free(new); + return -ENOMEM; + } + + list_add_tail(&new->list, &sl->list); + + return 0; +} + int string_list_add_sorted(struct string_list *sl, char *str) { struct string_list *new; |