[lttng-dev] [Babeltrace RFC PATCH 27/28] MinGW implementation of [v]asprintf

Ikaheimonen, JP jp_ikaheimonen at mentor.com
Thu May 2 08:07:50 EDT 2013


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

diff --git a/include/babeltrace/compat/stdio.h b/include/babeltrace/compat/stdio.h
index be64e60..ce5bab2 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,40 @@ static inline void funlockfile(FILE * filehandle)
 	return;
 }
 
+static inline
+int vasprintf(char ** strp, const char * fmt, va_list argv)
+{
+	int ret;
+
+	/* allocate even bigger chunks of memory until the string fits */
+	char * buf;
+	size_t size = 100;
+	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