strvprintf.c (1015B)
1 /************************************************************************* 2 * This file is part of Crosroads 1.23, a load balancer and fail over 3 * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL. 4 * Visit http://crossroads.e-tunity.com for information. 5 *************************************************************************/ 6 #include "crossroads.h" 7 8 #define STR_BLOCK 512 9 10 char *str_vprintf (char const *fmt, va_list arguments) { 11 int size = STR_BLOCK; /* initial size guess */ 12 char *buffer = xmalloc (size); /* initial buffer */ 13 int nchars; /* return value of vsnprintf */ 14 15 while (1) { /* try to make string */ 16 nchars = vsnprintf (buffer, size, fmt, arguments); 17 18 /* if this worked, return string */ 19 if (nchars > -1 && nchars < size) 20 return (buffer); 21 22 /* try again with more space */ 23 if (nchars > -1) 24 size = nchars + 1; 25 else 26 size += STR_BLOCK; 27 28 buffer = xrealloc (buffer, size); 29 } 30 31 return (0); /* to satisfy prototype */ 32 }