load.c 599 B

123456789101112131415161718192021222324252627282930313233
  1. #include <unistd.h>
  2. #include "open.h"
  3. #include "e.h"
  4. #include "load.h"
  5. static int readall(int fd,void *x,long long xlen)
  6. {
  7. long long r;
  8. while (xlen > 0) {
  9. r = xlen;
  10. if (r > 1048576) r = 1048576;
  11. r = read(fd,x,r);
  12. if (r == 0) errno = EPROTO;
  13. if (r <= 0) {
  14. if (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK) continue;
  15. return -1;
  16. }
  17. x += r;
  18. xlen -= r;
  19. }
  20. return 0;
  21. }
  22. int load(const char *fn,void *x,long long xlen)
  23. {
  24. int fd;
  25. int r;
  26. fd = open_read(fn);
  27. if (fd == -1) return -1;
  28. r = readall(fd,x,xlen);
  29. close(fd);
  30. return r;
  31. }