From be18d7193a37dc27c51691b5c76570e819f1f639 Mon Sep 17 00:00:00 2001 From: Guilherme Janczak <25590950+guijan@users.noreply.github.com> Date: Wed, 17 Mar 2021 03:01:00 +0000 Subject: [PATCH] cleaner tryread() --- src/allocator_thread.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/allocator_thread.c b/src/allocator_thread.c index b7b8c97..3e744cf 100644 --- a/src/allocator_thread.c +++ b/src/allocator_thread.c @@ -196,21 +196,18 @@ static int sendmessage(enum at_direction dir, struct at_msg *msg) { return ret; } -static int tryread(int fd, void* buf, size_t bytes) { +static int tryread(int fd, void *vbuf, size_t bytes) { ssize_t ret; - unsigned char *out = buf; -again: - ret = read(fd, out, bytes); - switch(ret) { - case -1: - if(errno == EINTR) goto again; - case 0: + unsigned char *buf = vbuf; + + for(;;) { + ret = read(fd, buf, bytes); + if (ret == -1 && errno != EINTR) return 0; - default: - if(ret == bytes || !bytes) return 1; - out += ret; - bytes -= ret; - goto again; + else if (ret == bytes) + return 1; + buf += ret; + bytes -= ret; } }