__stdio_read.c 611 B

123456789101112131415161718192021222324
  1. #include "stdio_impl.h"
  2. #include <sys/uio.h>
  3. size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
  4. {
  5. struct iovec iov[2] = {
  6. { .iov_base = buf, .iov_len = len - !!f->buf_size },
  7. { .iov_base = f->buf, .iov_len = f->buf_size }
  8. };
  9. ssize_t cnt;
  10. cnt = iov[0].iov_len ? syscall(SYS_readv, f->fd, iov, 2)
  11. : syscall(SYS_read, f->fd, iov[1].iov_base, iov[1].iov_len);
  12. if (cnt <= 0) {
  13. f->flags |= cnt ? F_ERR : F_EOF;
  14. return 0;
  15. }
  16. if (cnt <= iov[0].iov_len) return cnt;
  17. cnt -= iov[0].iov_len;
  18. f->rpos = f->buf;
  19. f->rend = f->buf + cnt;
  20. if (f->buf_size) buf[len-1] = *f->rpos++;
  21. return len;
  22. }