1570af302Sopenharmony_ci#include "stdio_impl.h"
2570af302Sopenharmony_ci#include <sys/uio.h>
3570af302Sopenharmony_ci
4570af302Sopenharmony_cisize_t __stdio_readx(FILE *f, unsigned char *buf, size_t len)
5570af302Sopenharmony_ci{
6570af302Sopenharmony_ci	ssize_t cnt = syscall(SYS_read, f->fd, buf, len);
7570af302Sopenharmony_ci	if (cnt <= 0) {
8570af302Sopenharmony_ci		f->flags |= cnt ? F_ERR : F_EOF;
9570af302Sopenharmony_ci		return 0;
10570af302Sopenharmony_ci	}
11570af302Sopenharmony_ci	return cnt;
12570af302Sopenharmony_ci}
13570af302Sopenharmony_ci
14570af302Sopenharmony_cisize_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
15570af302Sopenharmony_ci{
16570af302Sopenharmony_ci	struct iovec iov_buf[2] = {
17570af302Sopenharmony_ci		{ .iov_base = buf, .iov_len = len - !!f->buf_size },
18570af302Sopenharmony_ci		{ .iov_base = f->buf, .iov_len = f->buf_size }
19570af302Sopenharmony_ci	};
20570af302Sopenharmony_ci	ssize_t cnt;
21570af302Sopenharmony_ci
22570af302Sopenharmony_ci	cnt = iov_buf[0].iov_len ? syscall(SYS_readv, f->fd, iov_buf, 2)
23570af302Sopenharmony_ci		: syscall(SYS_read, f->fd, iov_buf[1].iov_base, iov_buf[1].iov_len);
24570af302Sopenharmony_ci	if (cnt <= 0) {
25570af302Sopenharmony_ci		f->flags |= cnt ? F_ERR : F_EOF;
26570af302Sopenharmony_ci		return 0;
27570af302Sopenharmony_ci	}
28570af302Sopenharmony_ci	if (cnt <= iov_buf[0].iov_len) {
29570af302Sopenharmony_ci		return cnt;
30570af302Sopenharmony_ci	}
31570af302Sopenharmony_ci	cnt -= iov_buf[0].iov_len;
32570af302Sopenharmony_ci	f->rpos = f->buf;
33570af302Sopenharmony_ci	f->rend = f->buf + cnt;
34570af302Sopenharmony_ci	if (f->buf_size) buf[len-1] = *f->rpos++;
35570af302Sopenharmony_ci	return len;
36570af302Sopenharmony_ci}
37