diff options
Diffstat (limited to 'drain.c')
-rw-r--r-- | drain.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -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); + } +} |