psx emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
685 B

  1. #include "util.h"
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. byte_arr_t *read_file(const char *filepath) {
  6. FILE *fp = fopen(filepath, "rb");
  7. byte_arr_t *arr = NULL;
  8. if (fp == NULL) {
  9. // TODO: Log correctly
  10. fprintf(stderr, "%s: ", filepath);
  11. perror("could not open");
  12. goto end;
  13. }
  14. fseek(fp, 0, SEEK_END);
  15. const long size = ftell(fp);
  16. if (size < 0) {
  17. goto end;
  18. }
  19. arr = malloc(sizeof *arr + size);
  20. if (arr == NULL) {
  21. goto end;
  22. }
  23. arr->size = size;
  24. rewind(fp);
  25. fread(arr->data, 1, size, fp);
  26. end:
  27. if (fp != NULL) {
  28. fclose(fp);
  29. }
  30. return arr;
  31. }