1 #include "stdio_impl.h"
4 static void cleanup(void *p)
7 if (!f->lockcount) __unlockfile(f);
10 size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
12 struct iovec iovs[2] = {
13 { .iov_base = f->wbase, .iov_len = f->wpos-f->wbase },
14 { .iov_base = (void *)buf, .iov_len = len }
16 struct iovec *iov = iovs;
17 size_t rem = iov[0].iov_len + iov[1].iov_len;
21 if (libc.main_thread) {
22 pthread_cleanup_push(cleanup, f);
23 cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
24 pthread_cleanup_pop(0);
26 cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
29 f->wend = f->buf + f->buf_size;
30 f->wpos = f->wbase = f->buf;
34 f->wpos = f->wbase = f->wend = 0;
36 return iovcnt == 2 ? 0 : len-iov[0].iov_len;
39 if (cnt > iov[0].iov_len) {
40 f->wpos = f->wbase = f->buf;
41 cnt -= iov[0].iov_len;
43 } else if (iovcnt == 2) {
46 iov[0].iov_base = (char *)iov[0].iov_base + cnt;
47 iov[0].iov_len -= cnt;