util: add save string concat with formated string

An example:
```c
char *txt = NULL;
for (int i = 0; i < 5; i++)
	xstrcatf(&txt, " %d", i);
puts(txt);
```
will produce the output:
` 0 1 2 3 4`

Signed-off-by: Denis Osterland-Heim <Denis.Osterland@diehl.com>
This commit is contained in:
Denis Osterland-Heim 2021-02-24 13:19:51 +01:00
parent 351d50e378
commit 344416f5d9
2 changed files with 18 additions and 0 deletions

View File

@ -17,6 +17,7 @@ void image_error(struct image *image, const char *fmt, ...) __attribute__ ((form
void image_info(struct image *image, const char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
void image_debug(struct image *image, const char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
void xasprintf(char **strp, const char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
void xstrcatf(char **strp, const char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
const char *imagepath(void);
const char *inputpath(void);

17
util.c
View File

@ -76,6 +76,23 @@ void xasprintf(char **strp, const char *fmt, ...)
va_end (args);
}
void xstrcatf(char **strp, const char *fmt, ...)
{
char *tmp;
va_list list;
va_start(list, fmt);
xvasprintf(&tmp, fmt, list);
va_end(list);
if (*strp) {
*strp = xrealloc(*strp, strlen(*strp) + strlen(tmp) + 1);
strcat(*strp, tmp);
free(tmp);
} else {
*strp = tmp;
}
}
static void image_log(struct image *image, int level, const char *fmt,
va_list args)
{