-static char *
-load_file (const char *filename, size_t *len_r)
-{
- int fd, r, start;
- char *content = NULL, *p;
- char buf[65536];
-
- *len_r = 0;
-
- fd = open (filename, O_RDONLY);
- if (fd == -1) {
- perror (filename);
- return NULL;
- }
-
- while ((r = read (fd, buf, sizeof buf)) > 0) {
- start = *len_r;
- *len_r += r;
- p = realloc (content, *len_r + 1);
- if (p == NULL) {
- perror ("realloc");
- free (content);
- return NULL;
- }
- content = p;
- memcpy (content + start, buf, r);
- content[start+r] = '\0';
- }
-
- if (r == -1) {
- perror (filename);
- free (content);
- return NULL;
- }
-
- if (close (fd) == -1) {
- perror (filename);
- free (content);
- return NULL;
- }
-
- return content;
-}
-