xref: /third_party/musl/src/stdio/__stdio_read.c (revision 570af302)
1#include "stdio_impl.h"
2#include <sys/uio.h>
3
4size_t __stdio_readx(FILE *f, unsigned char *buf, size_t len)
5{
6	ssize_t cnt = syscall(SYS_read, f->fd, buf, len);
7	if (cnt <= 0) {
8		f->flags |= cnt ? F_ERR : F_EOF;
9		return 0;
10	}
11	return cnt;
12}
13
14size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
15{
16	struct iovec iov_buf[2] = {
17		{ .iov_base = buf, .iov_len = len - !!f->buf_size },
18		{ .iov_base = f->buf, .iov_len = f->buf_size }
19	};
20	ssize_t cnt;
21
22	cnt = iov_buf[0].iov_len ? syscall(SYS_readv, f->fd, iov_buf, 2)
23		: syscall(SYS_read, f->fd, iov_buf[1].iov_base, iov_buf[1].iov_len);
24	if (cnt <= 0) {
25		f->flags |= cnt ? F_ERR : F_EOF;
26		return 0;
27	}
28	if (cnt <= iov_buf[0].iov_len) {
29		return cnt;
30	}
31	cnt -= iov_buf[0].iov_len;
32	f->rpos = f->buf;
33	f->rend = f->buf + cnt;
34	if (f->buf_size) buf[len-1] = *f->rpos++;
35	return len;
36}
37