master
1#include "stdio_impl.h"
2#include <sys/uio.h>
3
4size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
5{
6 struct iovec iovs[2] = {
7 { .iov_base = f->wbase, .iov_len = f->wpos-f->wbase },
8 { .iov_base = (void *)buf, .iov_len = len }
9 };
10 struct iovec *iov = iovs;
11 size_t rem = iov[0].iov_len + iov[1].iov_len;
12 int iovcnt = 2;
13 ssize_t cnt;
14 for (;;) {
15#ifdef __wasilibc_unmodified_upstream // WASI has no syscall
16 cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
17#else
18 cnt = writev(f->fd, iov, iovcnt);
19#endif
20 if (cnt == rem) {
21 f->wend = f->buf + f->buf_size;
22 f->wpos = f->wbase = f->buf;
23 return len;
24 }
25 if (cnt < 0) {
26 f->wpos = f->wbase = f->wend = 0;
27 f->flags |= F_ERR;
28 return iovcnt == 2 ? 0 : len-iov[0].iov_len;
29 }
30 rem -= cnt;
31 if (cnt > iov[0].iov_len) {
32 cnt -= iov[0].iov_len;
33 iov++; iovcnt--;
34 }
35 iov[0].iov_base = (char *)iov[0].iov_base + cnt;
36 iov[0].iov_len -= cnt;
37 }
38}