summaryrefslogtreecommitdiff
path: root/common/common.c
blob: d38f7acd41140df58eb8b676a08097c584f4d19d (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 "common.h"

#include <stdarg.h>
#include <stdio.h>

char *read_file(const char *path, size_t *size_out)
{
        FILE *file = fopen(path, "r");
        if (!file)
                log_fatal(ERR_IO, "could not open file '%s'", path);

        fseek(file, 0L, SEEK_END);
        size_t size = ftell(file);
        rewind(file);

        char *dat = mem_alloc(sizeof(char) * (size + 1));

        size_t bytes_read = fread(dat, sizeof(char), size, file);

        if (bytes_read < size) {
                log_fatal(ERR_IO, "could not read file '%s'", path);
        }

        fclose(file);

        if (size_out)
                *size_out = size;

        return dat;
}