[git commit] platform.c: provide getline implementation

Denys Vlasenko vda.linux at googlemail.com
Wed Jun 29 00:19:58 UTC 2011


commit: http://git.busybox.net/busybox/commit/?id=0a5b310067abfde9bf74a42352fc421e1c27e2b9
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

Signed-off-by: Timo Teras <timo.teras at iki.fi>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 include/platform.h |    5 +++++
 libbb/platform.c   |   29 +++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/include/platform.h b/include/platform.h
index cbe85f4..eafc3fc 100644
--- a/include/platform.h
+++ b/include/platform.h
@@ -350,6 +350,7 @@ typedef unsigned smalluint;
 #define HAVE_STRSIGNAL 1
 #define HAVE_STRVERSCMP 1
 #define HAVE_VASPRINTF 1
+#define HAVE_GETLINE 1
 #define HAVE_XTABS 1
 #define HAVE_MNTENT_H 1
 #define HAVE_NET_ETHERNET_H 1
@@ -470,4 +471,8 @@ extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
 extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
 #endif
 
+#ifndef HAVE_GETLINE
+extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
+#endif
+
 #endif
diff --git a/libbb/platform.c b/libbb/platform.c
index 04b8961..2bf34f5 100644
--- a/libbb/platform.c
+++ b/libbb/platform.c
@@ -145,3 +145,32 @@ char* FAST_FUNC stpcpy(char *p, const char *to_add)
 	return p;
 }
 #endif
+
+#ifndef HAVE_GETLINE
+ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
+{
+	int ch;
+	char *line = *lineptr;
+	size_t alloced = *n;
+	size_t len = 0;
+
+	do {
+		ch = fgetc(stream);
+		if (ch == EOF)
+			break;
+		if (len + 1 >= alloced) {
+			alloced += alloced/4 + 64;
+			line = xrealloc(line, alloced);
+		}
+		line[len++] = ch;
+	} while (ch != '\n');
+
+	if (len == 0)
+		return -1;
+
+	line[len] = '\0';
+	*lineptr = line;
+	*n = alloced;
+	return len;
+}
+#endif
-- 
1.7.3.4



More information about the busybox-cvs mailing list