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