diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2018-08-01 23:12:41 +0200 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2018-08-01 23:12:41 +0200 |
commit | a239f9feadd015fa91d391df01365dcade8ce503 (patch) | |
tree | 979554817c2069a525e94e2ad5f6346b53e19eb5 /util.c | |
parent | 9f2fccfdc2eae83efbde1e3ae94a2cc220537983 (diff) |
Threaded communication (icmpd)
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -1,8 +1,17 @@ #include <stdio.h> +#include <stdlib.h> #include <ctype.h> +#include <unistd.h> +#include <errno.h> +#include <assert.h> #include "util.h" +int uniqid(void) { + static int i = 0; + return i++; +} + void xxd(const void *buf_, size_t length) { unsigned char *buf = (unsigned char*)buf_; @@ -28,3 +37,31 @@ void xxd(const void *buf_, size_t length) { cursor += 16; } } + +ssize_t readall(int fd, void *data, size_t length) { + size_t cursor = 0; + while (cursor < length) { + ssize_t nr = read(fd, data + cursor, length - cursor); + if (nr < 0) { + if (errno == EINTR) continue; + return -1; + } + assert(nr > 0); + cursor += nr; + } + return length; +} + +ssize_t writeall(int fd, const void *data, size_t length) { + size_t cursor = 0; + while (cursor < length) { + ssize_t nw = write(fd, data + cursor, length - cursor); + if (nw < 0) { + if (errno == EINTR) continue; + return -1; + } + assert(nw > 0); + cursor += nw; + } + return length; +} |