loadfile.c 610 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * Copyright (c) 2002-2019 986-Studio.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <sys/mman.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #include <fcntl.h>
  13. /* Map a file in memory */
  14. void *LoadFilePtr(char *filename)
  15. {
  16. int fd;
  17. void *RetPtr;
  18. struct stat FileStat;
  19. fd = open(filename, O_RDONLY);
  20. fstat(fd, &FileStat);
  21. RetPtr = mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  22. close(fd);
  23. if (RetPtr == MAP_FAILED)
  24. {
  25. RetPtr = NULL;
  26. }
  27. return RetPtr;
  28. }