fix wide scanf's handling of input failure on %c, and simplify %[
[musl] / src / stdio / __stdio_read.c
1 #include "stdio_impl.h"
2 #include <pthread.h>
3
4 static void cleanup(void *p)
5 {
6         FILE *f = p;
7         if (!f->lockcount) __unlockfile(f);
8 }
9
10 size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
11 {
12         struct iovec iov[2] = {
13                 { .iov_base = buf, .iov_len = len - !!f->buf_size },
14                 { .iov_base = f->buf, .iov_len = f->buf_size }
15         };
16         ssize_t cnt;
17
18         pthread_cleanup_push(cleanup, f);
19         cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
20         pthread_cleanup_pop(0);
21         if (cnt <= 0) {
22                 f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
23                 f->rpos = f->rend = 0;
24                 return cnt;
25         }
26         if (cnt <= iov[0].iov_len) return cnt;
27         cnt -= iov[0].iov_len;
28         f->rpos = f->buf;
29         f->rend = f->buf + cnt;
30         if (f->buf_size) buf[len-1] = *f->rpos++;
31         return len;
32 }