diff src/gtkspell.c @ 2670:f6f2871d77c2

[gaim-migrate @ 2683] "just come a little closer" committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Sun, 04 Nov 2001 10:46:39 +0000
parents 9cee43e4903d
children f3c094e78609
line wrap: on
line diff
--- a/src/gtkspell.c	Sun Nov 04 07:42:28 2001 +0000
+++ b/src/gtkspell.c	Sun Nov 04 10:46:39 2001 +0000
@@ -76,26 +76,44 @@
 }
 
 static char *readline() {
-	int len = 1024;
-	gchar *buf = g_malloc(len);
-	int pos = 0;
-	do {
-		int val = read(fd_read[0], buf + pos, 1);
-		if (val <= 0) {
+	static gchar *buf = NULL;
+	char *end;
+	char *ret;
+	char *tmp;
+
+	/* read until we get a newline */
+	while (!buf || (end = strchr(buf, '\n')) == NULL) {
+		char space[1024];
+		int ret = read(fd_read[0], space, 1023);
+		if (ret < 0) {
 			error_print("read: %s\n", strerror(errno));
-			g_free(buf);
 			return NULL;
 		}
-		pos += val;
-		if (pos == len) {
-			len *= 2;
-			buf = g_realloc(buf, len);
-		}
-	} while (buf[pos - 1] != '\n');
+		space[ret] = 0;
+		if (buf) {
+			tmp = buf;
+			buf = g_strconcat(tmp, space, NULL);
+			g_free(tmp);
+		} else
+			buf = g_strdup(space);
+	}
+
+	/* we got a newline, and end points to it.
+	 * copy out the data, reset buf, return */
 
-	buf = g_realloc(buf, pos + 1);
-	buf[pos] = 0;
-	return buf;
+	if (end[1] == 0) {
+		/* only one line is in the buffer */
+		ret = buf;
+		buf = NULL;
+		return ret;
+	}
+
+	ret = g_strndup(buf, end - buf + 1);
+	tmp = buf;
+	buf = g_strdup(end + 1);
+	g_free(tmp);
+
+	return ret;
 }
 
 static char *readresponse() {