main rd.c
 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <unistd.h>
 4
 5char *fbuf(const char *file) {
 6  FILE *f = fopen(file, "rb");
 7
 8  if (!f)
 9    return NULL;
10
11  fseek(f, 0, SEEK_END);
12  long size = ftell(f);
13
14  if (size < 0) {
15    fclose(f);
16    return NULL;
17  }
18
19  rewind(f);
20
21  char *buf = malloc((size_t)size + 1);
22  if (!buf) {
23    fclose(f);
24    return NULL;
25  }
26
27  size_t readb = fread(buf, 1, (size_t)size, f);
28  fclose(f);
29
30  if (readb != (size_t)size) {
31    free(buf);
32    return NULL;
33  }
34
35  buf[size] = '\0';
36  return buf;
37}