#include "drain.h" #include #include 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); } }