X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fstdio%2F__stdio_read.c;h=ee17a576c5bb70c66c1def2d9d8f5537d7896059;hb=b7a2761780c28cd0167ce4e51623e42298096708;hp=d9bb3269c41821031be8e5c8e1d95020fbe03218;hpb=aa398f56fa398f2202b04e82c67f822f3233786f;p=musl diff --git a/src/stdio/__stdio_read.c b/src/stdio/__stdio_read.c index d9bb3269..ee17a576 100644 --- a/src/stdio/__stdio_read.c +++ b/src/stdio/__stdio_read.c @@ -1,6 +1,32 @@ #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) { - return syscall(SYS_read, f->fd, buf, len); + struct iovec iov[2] = { + { .iov_base = buf, .iov_len = len - !!f->buf_size }, + { .iov_base = f->buf, .iov_len = f->buf_size } + }; + ssize_t cnt; + + pthread_cleanup_push(cleanup, f); + cnt = syscall_cp(SYS_readv, f->fd, iov, 2); + pthread_cleanup_pop(0); + if (cnt <= 0) { + f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt); + f->rpos = f->rend = 0; + return cnt; + } + 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; }