diff options
author | unknown <knielsen@knielsen-hq.org> | 2012-08-24 15:29:01 +0200 |
---|---|---|
committer | unknown <knielsen@knielsen-hq.org> | 2012-08-24 15:29:01 +0200 |
commit | c5460fe7ec6c7083233b979c5d69876237eea5d6 (patch) | |
tree | 1189a55392ca290d6c9e8446250b7a9384feafb6 /strings | |
parent | b509a5e7bf161267f9e40258be84577b8d7210ed (diff) | |
parent | 7d116a49135d9dc9156b872d87f689ba00e50fee (diff) | |
download | mariadb-git-c5460fe7ec6c7083233b979c5d69876237eea5d6.tar.gz |
Merge from 5.3
Diffstat (limited to 'strings')
-rw-r--r-- | strings/my_vsnprintf.c | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/strings/my_vsnprintf.c b/strings/my_vsnprintf.c index 64c37ca0537..55d907acf1c 100644 --- a/strings/my_vsnprintf.c +++ b/strings/my_vsnprintf.c @@ -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; } |