X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;f=src%2Fcommon%2Fstring-utils%2Fstring-utils.c;h=80b9f73504f8cfba664429a87a721a113b9ebda1;hb=ea76a8bbde6896bddfa656627e8f12e0b9f55a5e;hp=526604d14e42a6f0d907fab86676ca2f2435e46f;hpb=5b770521bbf951df78c52ea4c2b7cf9e0dc1bd5e;p=lttng-tools.git diff --git a/src/common/string-utils/string-utils.c b/src/common/string-utils/string-utils.c index 526604d14..80b9f7350 100644 --- a/src/common/string-utils/string-utils.c +++ b/src/common/string-utils/string-utils.c @@ -7,10 +7,12 @@ #define _LGPL_SOURCE #include +#include #include #include #include #include +#include #include "string-utils.h" #include "../macros.h" @@ -392,3 +394,50 @@ int strutils_append_str(char **s, const char *append) free(old); return 0; } + +LTTNG_HIDDEN +int strutils_appendf(char **s, const char *fmt, ...) +{ + char *new_str; + size_t oldlen = (*s) ? strlen(*s) : 0; + int ret; + va_list args; + + /* Compute length of formatted string we append. */ + va_start(args, fmt); + ret = vsnprintf(NULL, 0, fmt, args); + va_end(args); + + if (ret == -1) { + goto end; + } + + /* Allocate space for old string + new string + \0. */ + new_str = calloc(oldlen + ret + 1, 1); + if (!new_str) { + ret = -ENOMEM; + goto end; + } + + /* Copy old string, if there was one. */ + if (oldlen) { + strcpy(new_str, *s); + } + + /* Format new string in-place. */ + va_start(args, fmt); + ret = vsprintf(&new_str[oldlen], fmt, args); + va_end(args); + + if (ret == -1) { + ret = -1; + goto end; + } + + free(*s); + *s = new_str; + new_str = NULL; + +end: + return ret; +}