fix nl_langinfo table for LC_TIME era-related items
[musl] / src / stdio / __stdio_write.c
1 #include "stdio_impl.h"
2 #include <sys/uio.h>
3 #include <pthread.h>
4
5 static void cleanup(void *p)
6 {
7         FILE *f = p;
8         if (!f->lockcount) __unlockfile(f);
9 }
10
11 size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
12 {
13         struct iovec iovs[2] = {
14                 { .iov_base = f->wbase, .iov_len = f->wpos-f->wbase },
15                 { .iov_base = (void *)buf, .iov_len = len }
16         };
17         struct iovec *iov = iovs;
18         size_t rem = iov[0].iov_len + iov[1].iov_len;
19         int iovcnt = 2;
20         ssize_t cnt;
21         for (;;) {
22                 pthread_cleanup_push(cleanup, f);
23                 cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
24                 pthread_cleanup_pop(0);
25                 if (cnt == rem) {
26                         f->wend = f->buf + f->buf_size;
27                         f->wpos = f->wbase = f->buf;
28                         return len;
29                 }
30                 if (cnt < 0) {
31                         f->wpos = f->wbase = f->wend = 0;
32                         f->flags |= F_ERR;
33                         return iovcnt == 2 ? 0 : len-iov[0].iov_len;
34                 }
35                 rem -= cnt;
36                 if (cnt > iov[0].iov_len) {
37                         f->wpos = f->wbase = f->buf;
38                         cnt -= iov[0].iov_len;
39                         iov++; iovcnt--;
40                 } else if (iovcnt == 2) {
41                         f->wbase += cnt;
42                 }
43                 iov[0].iov_base = (char *)iov[0].iov_base + cnt;
44                 iov[0].iov_len -= cnt;
45         }
46 }