+
+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;
+}