From 957c64c7b8b5e98d8a03dd84c7e27e7991fb9dbc Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Mon, 6 Apr 2026 17:04:05 -0400 Subject: Initial commit --- common/common.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 common/common.c (limited to 'common/common.c') diff --git a/common/common.c b/common/common.c new file mode 100644 index 0000000..d38f7ac --- /dev/null +++ b/common/common.c @@ -0,0 +1,30 @@ +#include "common.h" + +#include +#include + +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; +} -- cgit v1.3-2-g0d8e