#include #include #include #include #include "io.h" #include "types.h" i32 readfile(const char *path, char **data) { FILE *fp = NULL; i32 size = -1; *data = NULL; if (!(fp = fopen(path, "r"))) { goto cleanup; } if (fseek(fp, 0L, SEEK_END) != 0) { goto cleanup; } if ((size = ftell(fp)) < 0) { goto cleanup; } if (fseek(fp, 0L, SEEK_SET) != 0) { goto cleanup; } if ((*data = malloc((size_t)size + 1)) == NULL) { goto cleanup; } if (fread(*data, 1, (size_t)size, fp) != (size_t)size) { size = -1; goto cleanup; } (*data)[size] = '\0'; cleanup: if (fp) { fclose(fp); } if (size < 0 && *data) { free(*data); *data = NULL; } return size; } void _bail(const char *file, i32 line, const char *restrict fmt, ...) { va_list ap; va_start(ap, fmt); fflush(stdout); fprintf(stderr, "%s:%d: fatal: ", file, line); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); exit(1); }