#include "util.h"
|
|
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void fatal(const char *fmt, ...)
|
|
{
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
vfprintf(stderr, fmt, args);
|
|
fprintf(stderr, "\n");
|
|
va_end(args);
|
|
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
byte_arr_t *read_file(const char *filepath)
|
|
{
|
|
FILE *fp = fopen(filepath, "rb");
|
|
byte_arr_t *arr = NULL;
|
|
|
|
if (fp == NULL)
|
|
{
|
|
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;
|
|
}
|