Subject: [PATCH 5/9] Introduce strbuf_read_fd().

From: Kristian Høgsberg <[email-masked]>
Date: Wed, 5 Sep 2007 20:23:34 -0400

This function reads from a given fd into a strbuf until end of file.

Signed-off-by: Kristian Høgsberg <[email-masked]>
---
 strbuf.c |   19 +++++++++++++++++++
 strbuf.h |    1 +
 2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index 2805c11..fcfc05e 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -50,6 +50,25 @@ void read_line(struct strbuf *sb, FILE *fp, int term) {
 	strbuf_end(sb);
 }
 
+int strbuf_read_fd(struct strbuf *sb, int fd)
+{
+	int len, total = 0;
+
+	do {
+		strbuf_grow(sb, 1024);
+		len = xread(fd, sb->buf + sb->len, sb->alloc - sb->len);
+		if (len > 0) {
+			total += len;
+			sb->len += len;
+		}
+	} while (len > 0);
+
+	if (len < 0)
+		return len;
+
+	return total;
+}
+
 void strbuf_printf(struct strbuf *sb, const char *fmt, ...)
 {
 	char buffer[2048];
diff --git a/strbuf.h b/strbuf.h
index 1e5d09e..6e630ea 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -12,5 +12,6 @@ extern void read_line(struct strbuf *, FILE *, int);
 extern void strbuf_add(struct strbuf *sb, const char *data, size_t len);
 extern void strbuf_add_char(struct strbuf *sb, int ch);
 extern void strbuf_printf(struct strbuf *sb, const char *fmt, ...);
+extern int strbuf_read_fd(struct strbuf *sb, int fd);
 
 #endif /* STRBUF_H */
-- 
1.5.2.GIT