#include "sprintf_alloc.h"
#include "libbb/libbb.h"
-void
-sprintf_alloc(char **str, const char *fmt, ...)
+void sprintf_alloc(char **str, const char *fmt, ...)
{
va_list ap;
int n;
- unsigned int size = 1;
+ unsigned int size = 0;
- *str = xcalloc(1, size);
+ *str = NULL;
for (;;) {
va_start(ap, fmt);
- n = vsnprintf (*str, size, fmt, ap);
+ n = vsnprintf(*str, size, fmt, ap);
va_end(ap);
if (n < 0) {
fprintf(stderr, "%s: encountered an output or encoding"
- " error during vsnprintf.\n",
- __FUNCTION__);
+ " error during vsnprintf.\n", __FUNCTION__);
exit(EXIT_FAILURE);
}
break;
/* Truncated, try again with more space. */
- size = n+1;
+ size = n + 1;
*str = xrealloc(*str, size);
}
}