[lttng-dev] [Babeltrace RFC PATCH 26/28] Add MinGW implementation of getline

Ikaheimonen, JP jp_ikaheimonen at mentor.com
Thu May 2 08:06:51 EDT 2013


---
 compat/compat_strlib.c             | 40 ++++++++++++++++++++++++++++++++++++++
 include/babeltrace/compat/string.h |  2 ++
 2 files changed, 42 insertions(+)

diff --git a/compat/compat_strlib.c b/compat/compat_strlib.c
index 980a8f5..181cf66 100644
--- a/compat/compat_strlib.c
+++ b/compat/compat_strlib.c
@@ -2,6 +2,10 @@
 
 #ifdef __MINGW32__
 
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+
 char* strtok_r(
 	char *str,
 	const char *delim,
@@ -73,6 +77,42 @@ size_t strnlen(const char *str, size_t maxlen)
 	return len;
 }
 
+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;
+}
+
 #endif
 
 int compat_strerror_r(int errnum, char *buf, size_t buflen)
diff --git a/include/babeltrace/compat/string.h b/include/babeltrace/compat/string.h
index 82f411e..50f6ba1 100644
--- a/include/babeltrace/compat/string.h
+++ b/include/babeltrace/compat/string.h
@@ -2,10 +2,12 @@
 #define BABELTRACE_COMPAT_STRING_H
 
 #include <string.h>
+#include <stdio.h>
 
 #ifdef __MINGW32__
 char *strtok_r(char *str, const char *delim, char **nextp);
 size_t strnlen(const char *str, size_t maxlen);
+ssize_t getline(char **buf, size_t *len, FILE *stream);
 #endif
 
 int compat_strerror_r(int errnum, char *buf, size_t buflen);
-- 
1.8.1.msysgit.1




More information about the lttng-dev mailing list