blob: 40c049e7c2959193f7623fa2747a1fa9e93d92e9 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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);
}
}
|