summaryrefslogtreecommitdiff
path: root/drain.c
diff options
context:
space:
mode:
Diffstat (limited to 'drain.c')
-rw-r--r--drain.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/drain.c b/drain.c
new file mode 100644
index 0000000..40c049e
--- /dev/null
+++ b/drain.c
@@ -0,0 +1,28 @@
+#include "drain.h"
+#include <stdlib.h>
+#include <string.h>
+
+char *drain(FILE *fp)
+{
+ size_t size = 1025;
+ char *buf = calloc(1025, 1);
+ if (buf == NULL) {
+ perror("drain");
+ return NULL;
+ }
+ /* Read until end of file. */
+ for (;;) {
+ if (fread(buf + size - 1025, 1024, 1, fp) != 1)
+ return buf;
+ /* Line is longer than 1024 bytes, realloc to make space. */
+ size += 1024;
+ char *new_buf = realloc(buf, size);
+ if (new_buf == NULL) {
+ perror("drain");
+ free(buf);
+ return NULL;
+ }
+ buf = new_buf;
+ memset(buf + size - 1024, 0, 1024);
+ }
+}