summaryrefslogtreecommitdiff
path: root/drain.c
blob: ae403e7cb783832a9a540532b8d205232c497725 (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
29
30
#include "drain.h"
#include <stdio.h>
#include <stdlib.h>

char *
drain(FILE *fp)
{
	if (fseek(fp, 0, SEEK_END) < 0) {
		perror("drain:SEEK_END");
		return NULL;
	}
	long size = ftell(fp);
	if (fseek(fp, 0, SEEK_SET) < 0) {
		perror("drain:SEEK_SET");
		return NULL;
	}
	char *data = malloc(size + 1);
	if (data == NULL) {
		perror("drain:malloc");
		return NULL;
	}
	if ((long)fread(data, 1, size, fp) != size) {
		perror("drain:fread");
		free(data);
		return NULL;
	}
	data[size] = '\0';
	return data;
}