X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2F__stdio_read.c;h=c99ca9a913314bdb15c221943327a39e50dc2a75;hb=5271ff46b9e983bec5fd9ab79d5aaf096fa54157;hp=a2e4cd62fe9f35dbae98d1ade1fec32d7fe4ebf1;hpb=e3cd6c5c265cd481db6e0c5b529855d99f0bda30;p=musl diff --git a/src/stdio/__stdio_read.c b/src/stdio/__stdio_read.c index a2e4cd62..c99ca9a9 100644 --- a/src/stdio/__stdio_read.c +++ b/src/stdio/__stdio_read.c @@ -1,22 +1,36 @@ #include "stdio_impl.h" +#include + +static void cleanup(void *p) +{ + FILE *f = p; + if (!f->lockcount) __unlockfile(f); +} size_t __stdio_read(FILE *f, unsigned char *buf, size_t len) { struct iovec iov[2] = { - { .iov_base = buf, .iov_len = len }, + { .iov_base = buf, .iov_len = len - !!f->buf_size }, { .iov_base = f->buf, .iov_len = f->buf_size } }; ssize_t cnt; - cnt = syscall(SYS_readv, f->fd, iov, 2); + if (libc.main_thread) { + pthread_cleanup_push(cleanup, f); + cnt = syscall_cp(SYS_readv, f->fd, iov, 2); + pthread_cleanup_pop(0); + } else { + cnt = syscall(SYS_readv, f->fd, iov, 2); + } if (cnt <= 0) { f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt); f->rpos = f->rend = 0; return cnt; } - if (cnt <= len) return cnt; - cnt -= len; + if (cnt <= iov[0].iov_len) return cnt; + cnt -= iov[0].iov_len; f->rpos = f->buf; f->rend = f->buf + cnt; + if (f->buf_size) buf[len-1] = *f->rpos++; return len; }