summaryrefslogtreecommitdiff
path: root/strings
diff options
context:
space:
mode:
authorunknown <knielsen@knielsen-hq.org>2012-08-24 12:32:46 +0200
committerunknown <knielsen@knielsen-hq.org>2012-08-24 12:32:46 +0200
commit96703a63da2211cefcc480fa4f135acd94743622 (patch)
treea95df690fd2ca5cf82947213f2da5ad653fc26c1 /strings
parent60561ae6133cf40f4fc445e1d6e8f395a20b2573 (diff)
parentcdeabcfd436c65e0a97e74b1722d0259ba907541 (diff)
downloadmariadb-git-96703a63da2211cefcc480fa4f135acd94743622.tar.gz
Merge from 5.1.
Diffstat (limited to 'strings')
-rw-r--r--strings/my_vsnprintf.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/strings/my_vsnprintf.c b/strings/my_vsnprintf.c
index f2dd3f8e525..ea07d874f0f 100644
--- a/strings/my_vsnprintf.c
+++ b/strings/my_vsnprintf.c
@@ -636,3 +636,66 @@ size_t my_snprintf(char* to, size_t n, const char* fmt, ...)
return result;
}
+
+/**
+ Writes output to the stream according to a format string.
+
+ @param stream file to write to
+ @param format string format
+ @param args list of parameters
+
+ @retval
+ number of the characters written.
+*/
+
+int my_vfprintf(FILE *stream, const char* format, va_list args)
+{
+ char cvtbuf[1024];
+ 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;
+}