summaryrefslogtreecommitdiff
path: root/src/stdio/__stdio_write.c
diff options
context:
space:
mode:
authorVladimir Azarov <avm@intermediate-node.net>2024-10-01 15:47:05 +0200
committerVladimir Azarov <avm@intermediate-node.net>2024-10-01 15:47:05 +0200
commit4abab5ad6c8465a7528ccdd5f49367da05f78bbd (patch)
treeebf009bf1376a5a223a915bc27cbbd791a1316bc /src/stdio/__stdio_write.c
Initial version
Diffstat (limited to 'src/stdio/__stdio_write.c')
-rw-r--r--src/stdio/__stdio_write.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c
new file mode 100644
index 0000000..d2d8947
--- /dev/null
+++ b/src/stdio/__stdio_write.c
@@ -0,0 +1,34 @@
+#include "stdio_impl.h"
+#include <sys/uio.h>
+
+size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
+{
+ 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 (;;) {
+ 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) {
+ cnt -= iov[0].iov_len;
+ iov++; iovcnt--;
+ }
+ iov[0].iov_base = (char *)iov[0].iov_base + cnt;
+ iov[0].iov_len -= cnt;
+ }
+}