|
|
- #include "util.h"
-
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- byte_arr_t *read_file(const char *filepath)
- {
- FILE *fp = fopen(filepath, "rb");
- byte_arr_t *arr = NULL;
-
- if (fp == NULL)
- {
- // TODO: Log correctly
- fprintf(stderr, "%s: ", filepath);
- perror("could not open");
- goto end;
- }
-
- fseek(fp, 0, SEEK_END);
- const long size = ftell(fp);
- if (size < 0)
- {
- goto end;
- }
-
- arr = malloc(sizeof *arr + size);
- if (arr == NULL)
- {
- goto end;
- }
-
- arr->size = size;
-
- rewind(fp);
- fread(arr->data, 1, size, fp);
-
- end:
- if (fp != NULL)
- {
- fclose(fp);
- }
- return arr;
- }
|