summaryrefslogtreecommitdiff
path: root/src/stdio/__stdio_read.c
blob: a2e4cd62fe9f35dbae98d1ade1fec32d7fe4ebf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdio_impl.h"

size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
{
	struct iovec iov[2] = {
		{ .iov_base = buf, .iov_len = len },
		{ .iov_base = f->buf, .iov_len = f->buf_size }
	};
	ssize_t cnt;

	cnt = syscall(SYS_readv, f->fd, iov, 2);
	if (cnt <= 0) {
		f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
		f->rpos = f->rend = 0;
		return cnt;
	}
	if (cnt <= len) return cnt;
	cnt -= len;
	f->rpos = f->buf;
	f->rend = f->buf + cnt;
	return len;
}