1 /* sprintf_alloc.c -- like sprintf with memory allocation
3 Copyright (C) 2010 Ubiq Technologies <graham.gower@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
18 #include "sprintf_alloc.h"
19 #include "libbb/libbb.h"
22 sprintf_alloc(char **str
, const char *fmt
, ...)
26 unsigned int size
= 1;
28 *str
= xcalloc(1, size
);
32 n
= vsnprintf (*str
, size
, fmt
, ap
);
36 fprintf(stderr
, "%s: encountered an output or encoding"
37 " error during vsnprintf.\n",
45 /* Truncated, try again with more space. */
47 *str
= xrealloc(*str
, size
);