Debian Codegraph @

libc-top-half/musl/src/stdio/__stdio_read.c

raw
1 #ifdef __wasilibc_unmodified_upstream // WASI has no syscall
2 #else
3 #include <unistd.h>
4 #endif
5 #include "stdio_impl.h"
6 #include <sys/uio.h>
7
8 size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
9 {
10 struct iovec iov[2] = {
11 { .iov_base = buf, .iov_len = len - !!f->buf_size },
12 { .iov_base = f->buf, .iov_len = f->buf_size }
13 };
14 ssize_t cnt;
15
16 #ifdef __wasilibc_unmodified_upstream // WASI has no syscall
17 cnt = iov[0].iov_len ? syscall(SYS_readv, f->fd, iov, 2)
18 : syscall(SYS_read, f->fd, iov[1].iov_base, iov[1].iov_len);
19 #else
20 cnt = iov[0].iov_len ? readv(f->fd, iov, 2)
21 : read(f->fd, iov[1].iov_base, iov[1].iov_len);
22 #endif
23 if (cnt <= 0) {
24 f->flags |= cnt ? F_ERR : F_EOF;
25 return 0;
26 }
27 if (cnt <= iov[0].iov_len) return cnt;
28 cnt -= iov[0].iov_len;
29 f->rpos = f->buf;
30 f->rend = f->buf + cnt;
31 if (f->buf_size) buf[len-1] = *f->rpos++;
32 return len;
33 }