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.

44 lines
631 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. {
  7. FILE *fp = fopen(filepath, "rb");
  8. byte_arr_t *arr = NULL;
  9. if (fp == NULL)
  10. {
  11. // TODO: Log correctly
  12. fprintf(stderr, "%s: ", filepath);
  13. perror("could not open");
  14. goto end;
  15. }
  16. fseek(fp, 0, SEEK_END);
  17. const long size = ftell(fp);
  18. if (size < 0)
  19. {
  20. goto end;
  21. }
  22. arr = malloc(sizeof *arr + size);
  23. if (arr == NULL)
  24. {
  25. goto end;
  26. }
  27. arr->size = size;
  28. rewind(fp);
  29. fread(arr->data, 1, size, fp);
  30. end:
  31. if (fp != NULL)
  32. {
  33. fclose(fp);
  34. }
  35. return arr;
  36. }