mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +03:00
Merge from 5.3
This commit is contained in:
@ -694,6 +694,51 @@ size_t my_snprintf(char* to, size_t n, const char* fmt, ...)
|
||||
int my_vfprintf(FILE *stream, const char* format, va_list args)
|
||||
{
|
||||
char cvtbuf[1024];
|
||||
(void) my_vsnprintf(cvtbuf, sizeof(cvtbuf), format, args);
|
||||
return fprintf(stream, "%s\n", cvtbuf);
|
||||
int alloc= 0;
|
||||
char *p= cvtbuf;
|
||||
size_t cur_len= sizeof(cvtbuf);
|
||||
int ret;
|
||||
|
||||
/*
|
||||
We do not know how much buffer we need.
|
||||
So start with a reasonably-sized stack-allocated buffer, and increase
|
||||
it exponentially until it is big enough.
|
||||
*/
|
||||
for (;;)
|
||||
{
|
||||
size_t new_len;
|
||||
size_t actual= my_vsnprintf(p, cur_len, format, args);
|
||||
if (actual < cur_len - 1)
|
||||
break;
|
||||
/*
|
||||
Not enough space (or just enough with nothing to spare - but we cannot
|
||||
distinguish this case from the return value). Allocate a bigger buffer
|
||||
and try again.
|
||||
*/
|
||||
if (alloc)
|
||||
(*my_str_free)(p);
|
||||
else
|
||||
alloc= 1;
|
||||
new_len= cur_len*2;
|
||||
if (new_len < cur_len)
|
||||
return 0; /* Overflow */
|
||||
cur_len= new_len;
|
||||
p= (*my_str_malloc)(cur_len);
|
||||
if (!p)
|
||||
return 0;
|
||||
}
|
||||
ret= fprintf(stream, "%s", p);
|
||||
if (alloc)
|
||||
(*my_str_free)(p);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int my_fprintf(FILE *stream, const char* format, ...)
|
||||
{
|
||||
int result;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
result= my_vfprintf(stream, format, args);
|
||||
va_end(args);
|
||||
return result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user