aboutsummaryrefslogtreecommitdiff
path: root/async.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2019-11-04 12:45:38 +0000
committerDaniel Eklöf <daniel@ekloef.se>2019-11-04 12:46:30 +0000
commit9ae5c311d1b0cb85e7cbcb86fef91827e9967868 (patch)
tree0975f00b908e2612077acc5a5d71a7d0202e2fed /async.c
parentselection: send primary/clipboard data asynchronously (diff)
async: add async_write(), a write primitive to write to a NONBLOCK:ing FD
Diffstat (limited to 'async.c')
-rw-r--r--async.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/async.c b/async.c
new file mode 100644
index 00000000..3ad2a8c6
--- /dev/null
+++ b/async.c
@@ -0,0 +1,35 @@
+#include "async.h"
+
+#include <stdint.h>
+#include <errno.h>
+#include <unistd.h>
+
+#define LOG_MODULE "async"
+#define LOG_ENABLE_DBG 0
+#include "log.h"
+
+enum async_write_status
+async_write(int fd, const void *_data, size_t len, size_t *idx)
+{
+ const uint8_t *const data = _data;
+ size_t left = len - *idx;
+
+ while (left > 0) {
+ ssize_t ret = write(fd, &data[*idx], left);
+
+ if (ret < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK)
+ return ASYNC_WRITE_REMAIN;
+
+ return ASYNC_WRITE_ERR;
+ }
+
+ LOG_DBG("wrote %zd bytes of %zu (%zu left) to FD=%d",
+ ret, left, left - ret, fd);
+
+ *idx += ret;
+ left -= ret;
+ }
+
+ return ASYNC_WRITE_DONE;
+}