github.com/afumu/libc@v0.0.6/musl/src/stdio/__stdio_read.c (about)

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