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"
21 void sprintf_alloc(char **str
, const char *fmt
, ...)
25 unsigned int size
= 0;
31 n
= vsnprintf(*str
, size
, fmt
, ap
);
35 fprintf(stderr
, "%s: encountered an output or encoding"
36 " error during vsnprintf.\n", __FUNCTION__
);
43 /* Truncated, try again with more space. */
45 *str
= xrealloc(*str
, size
);