X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fstring-utils%2Fstring-utils.cpp;h=5cfcef2566fec7f3bf5173eb176ddab8cf1735d5;hb=c9e313bc594f40a86eed237dce222c0fc99c957f;hp=8dcc6b2aae9ce9fab7b3beeefbea0b495fc15db4;hpb=4ff750609549a99f6b318ce600c42c90d4f2e480;p=lttng-tools.git diff --git a/src/common/string-utils/string-utils.cpp b/src/common/string-utils/string-utils.cpp index 8dcc6b2aa..5cfcef256 100644 --- a/src/common/string-utils/string-utils.cpp +++ b/src/common/string-utils/string-utils.cpp @@ -1,20 +1,22 @@ /* * Copyright (C) 2017 Philippe Proulx * - * SPDX-License-Identifier: GPL-2.0-only + * SPDX-License-Identifier: LGPL-2.1-only * */ #define _LGPL_SOURCE #include +#include #include #include #include #include #include +#include -#include "string-utils.h" -#include "../macros.h" +#include "string-utils.hpp" +#include "../macros.hpp" enum star_glob_pattern_type_flags { STAR_GLOB_PATTERN_TYPE_FLAG_NONE = 0, @@ -64,7 +66,7 @@ void strutils_normalize_star_glob_pattern(char *pattern) goto end; } - /* Fall through default case. */ + /* fall through */ default: got_star = false; break; @@ -395,3 +397,49 @@ int strutils_append_str(char **s, const char *append) free(old); return 0; } + +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 = (char *) zmalloc(oldlen + ret + 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; +}