From 4e6b78ca29d221e04d8a0741672edb105adeeae1 Mon Sep 17 00:00:00 2001 From: chme Date: Sun, 9 Apr 2017 09:16:00 +0200 Subject: [PATCH] [misc] Add string_printf function --- src/misc.c | 23 +++++++++++++++++++++++ src/misc.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/src/misc.c b/src/misc.c index 0f7c5130..38b9e0a5 100644 --- a/src/misc.c +++ b/src/misc.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #ifndef CLOCK_REALTIME @@ -1132,3 +1133,25 @@ log_fatal_null(int domain, const char *func, int line) DPRINTF(E_FATAL, domain, "%s returned NULL at line %d\n", func, line); abort(); } + +/* + * Wrapper function for vasprintf by Intel Corporation + * Published under the L-GPL 2.1 licence as part of clr-boot-manager + * + * https://github.com/clearlinux/clr-boot-manager + */ +char *string_printf(const char *fmt, ...) +{ + char *ret = NULL; + va_list va; + va_start(va, fmt); + if (vasprintf(&ret, fmt, va) < 0) + { + DPRINTF(E_FATAL, L_MISC, "Out of memory for string_printf\n"); + abort(); + } + va_end(va); + return ret; +} + + diff --git a/src/misc.h b/src/misc.h index 685a3e14..f9e09052 100644 --- a/src/misc.h +++ b/src/misc.h @@ -196,4 +196,6 @@ void log_fatal_err(int domain, const char *func, int line, int err); void log_fatal_errno(int domain, const char *func, int line); void log_fatal_null(int domain, const char *func, int line); +char *string_printf(const char *fmt, ...); + #endif /* !__MISC_H__ */