diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2024-04-02 19:32:13 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2024-04-07 13:18:13 +0200 |
commit | 2cf66de4236ca05024917c183c24f4dede4aa6d9 (patch) | |
tree | 204d66805a8e955a7ba8f2319d551db4e7c0b9c3 /src/core/core-string.c | |
parent | 08bc6404eb0d7d6fb276c33d9f4cd6499c63d624 (diff) | |
download | weechat-2cf66de4236ca05024917c183c24f4dede4aa6d9.zip |
api: add function "asprintf"
Diffstat (limited to 'src/core/core-string.c')
-rw-r--r-- | src/core/core-string.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/core/core-string.c b/src/core/core-string.c index 572e6d1cd..4b8f3c957 100644 --- a/src/core/core-string.c +++ b/src/core/core-string.c @@ -71,6 +71,61 @@ char **string_concat_buffer[STRING_NUM_CONCAT_BUFFERS]; /* + * Formats a message in a string allocated by the function. + * + * This function is defined for systems where the GNU function `asprintf()` + * is not available. + * The behavior is almost the same except that `*result` is set to NULL on error. + * + * Returns the number of bytes in the resulting string, negative value in case + * of error. + * + * Value of `*result` is allocated with the result string (NULL if error), + * it must be freed after use. + */ + +int +string_asprintf (char **result, const char *fmt, ...) +{ + va_list argptr; + int num_bytes; + size_t size; + + if (!result) + return -1; + + *result = NULL; + + if (!fmt) + return -1; + + /* determine required size */ + va_start (argptr, fmt); + num_bytes = vsnprintf (NULL, 0, fmt, argptr); + va_end (argptr); + + if (num_bytes < 0) + return num_bytes; + + size = (size_t)num_bytes + 1; + *result = malloc (size); + if (!*result) + return -1; + + va_start (argptr, fmt); + num_bytes = vsnprintf (*result, size, fmt, argptr); + va_end (argptr); + + if (num_bytes < 0) + { + free (*result); + *result = NULL; + } + + return num_bytes; +} + +/* * Defines a "strndup" function for systems where this function does not exist * (FreeBSD and maybe others). * |