[lttng-dev] [Babeltrace PATCH 21/23] Add MinGW implementation of getline

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


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

diff --git a/include/babeltrace/compat/string.h b/include/babeltrace/compat/string.h
index 14e7b53..e2612f4 100644
--- a/include/babeltrace/compat/string.h
+++ b/include/babeltrace/compat/string.h
@@ -5,6 +5,10 @@
 
 #ifdef __MINGW32__
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
 static inline char* strtok_r(
 	char *str,
 	const char *delim,
@@ -79,6 +83,44 @@ static inline int compat_strerror_r(int errnum, char *buf, size_t buflen)
 	buf[buflen - 1] = '\0';
 	return 0;
 }
+
+static inline
+ssize_t getline(char **buf, size_t *len, FILE *stream)
+{
+	int c = EOF;
+	ssize_t count = (ssize_t)0;
+
+	if( feof( stream ) || ferror( stream ) )
+		return -1;
+
+	for(;;) {
+		/* read character from stream */
+		c = fgetc(stream);
+		if (c == EOF) {
+			break;
+		}
+		if (c == '\n') {
+			break;
+		}
+
+		/* store to *buf */
+		/* check if it fits - space for character and NUL */
+		if (count > *len - 2) {
+			/* have to allocate more space */
+			*len = *len + 100;
+			*buf = realloc(*buf, *len);
+			if (*buf == NULL) {
+				errno = ENOMEM;
+				return (ssize_t)-1;
+			}
+		}
+		(*buf)[count] = c;
+		count++;
+	}
+	(*buf)[count] = '\0';
+	return (count == (ssize_t)0) ? (ssize_t)-1 : (ssize_t)0;
+}
+
 #elif !defined(__linux__) || ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE))
 
 /* XSI-compliant strerror_r */
-- 
1.8.1.msysgit.1




More information about the lttng-dev mailing list