[lttng-dev] [Babeltrace PATCH 22/23] MinGW implementation of [v]asprintf

Ikaheimonen, JP jp_ikaheimonen at mentor.com
Wed May 22 04:08:21 EDT 2013


---
 include/babeltrace/compat/stdio.h | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/include/babeltrace/compat/stdio.h b/include/babeltrace/compat/stdio.h
index be64e60..cce3c70 100644
--- a/include/babeltrace/compat/stdio.h
+++ b/include/babeltrace/compat/stdio.h
@@ -4,6 +4,10 @@
 #include <stdio.h>
 
 #ifdef __MINGW32__
+
+#include <stdlib.h>
+#include <stdarg.h>
+
 static inline void flockfile (FILE * filehandle)
 {
 	return;
@@ -13,6 +17,41 @@ static inline void funlockfile(FILE * filehandle)
 	return;
 }
 
+static inline
+int vasprintf(char ** strp, const char * fmt, va_list argv)
+{
+	int ret;
+	char * buf;
+	size_t size = 100;
+
+	/* allocate even bigger chunks of memory until the string fits */
+	for(;;) {
+		buf = malloc(size);
+		if (buf == (char *)0) {
+			ret = -1;
+			break;
+		}
+		ret = vsnprintf(buf, size, fmt, argv);
+		if (ret < size)
+			break;
+		size = size + 100;
+		free(buf);
+	}
+	return ret;
+}
+
+static inline
+int asprintf(char ** strp, const char *fmt, ...)
+{
+	int ret;
+	va_list argv;
+
+	va_start(argv, fmt);
+	ret = vasprintf(strp, fmt, argv);
+	va_end(argv);
+	return ret;
+}
+
 #endif
 
 #endif
-- 
1.8.1.msysgit.1




More information about the lttng-dev mailing list