X-Git-Url: http://nsz.repo.hu/git/?p=musl;a=blobdiff_plain;f=src%2Fstdio%2F__stdio_write.c;h=cef7bbdc37238cbc8607a0e09e68f1551567d1e5;hp=785456265ac6f33910fa343dfff55d355b1e809d;hb=8c0a3d9e5c169fc9d0f246ab59362b658b029ad7;hpb=0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c index 78545626..cef7bbdc 100644 --- a/src/stdio/__stdio_write.c +++ b/src/stdio/__stdio_write.c @@ -1,9 +1,49 @@ #include "stdio_impl.h" +#include + +static void cleanup(void *p) +{ + FILE *f = p; + if (!f->lockcount) __unlockfile(f); +} size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len) { - const unsigned char *stop = buf+len; - ssize_t cnt = 1; - for (; buffd, buf, len))>0; buf+=cnt); - return len-(stop-buf); + struct iovec iovs[2] = { + { .iov_base = f->wbase, .iov_len = f->wpos-f->wbase }, + { .iov_base = (void *)buf, .iov_len = len } + }; + struct iovec *iov = iovs; + size_t rem = iov[0].iov_len + iov[1].iov_len; + int iovcnt = 2; + ssize_t cnt; + for (;;) { + if (libc.main_thread) { + pthread_cleanup_push(cleanup, f); + cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt); + pthread_cleanup_pop(0); + } else { + cnt = syscall(SYS_writev, f->fd, iov, iovcnt); + } + if (cnt == rem) { + f->wend = f->buf + f->buf_size; + f->wpos = f->wbase = f->buf; + return len; + } + if (cnt < 0) { + f->wpos = f->wbase = f->wend = 0; + f->flags |= F_ERR; + return iovcnt == 2 ? 0 : len-iov[0].iov_len; + } + rem -= cnt; + if (cnt > iov[0].iov_len) { + f->wpos = f->wbase = f->buf; + cnt -= iov[0].iov_len; + iov++; iovcnt--; + } else if (iovcnt == 2) { + f->wbase += cnt; + } + iov[0].iov_base = (char *)iov[0].iov_base + cnt; + iov[0].iov_len -= cnt; + } }