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.

55 lines
777 B

  1. #include "util.h"
  2. #include <stdarg.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. void fatal(const char *fmt, ...)
  6. {
  7. va_list args;
  8. va_start(args, fmt);
  9. vfprintf(stderr, fmt, args);
  10. fprintf(stderr, "\n");
  11. va_end(args);
  12. exit(EXIT_FAILURE);
  13. }
  14. byte_arr_t *read_file(const char *filepath)
  15. {
  16. FILE *fp = fopen(filepath, "rb");
  17. byte_arr_t *arr = NULL;
  18. if (fp == NULL)
  19. {
  20. fprintf(stderr, "%s: ", filepath);
  21. perror("could not open");
  22. goto end;
  23. }
  24. fseek(fp, 0, SEEK_END);
  25. const long size = ftell(fp);
  26. if (size < 0)
  27. {
  28. goto end;
  29. }
  30. arr = malloc(sizeof *arr + size);
  31. if (arr == NULL)
  32. {
  33. goto end;
  34. }
  35. arr->size = size;
  36. rewind(fp);
  37. fread(arr->data, 1, size, fp);
  38. end:
  39. if (fp != NULL)
  40. {
  41. fclose(fp);
  42. }
  43. return arr;
  44. }