From 50f3c688a7cd4819680dbd19b9933c6cb5e62ca7 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 6 Feb 2012 19:44:47 -0500 Subject: Move most files into lib, add top-level makefile --- lib/src/io.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/src/io.c (limited to 'lib/src/io.c') diff --git a/lib/src/io.c b/lib/src/io.c new file mode 100644 index 0000000..dcc055f --- /dev/null +++ b/lib/src/io.c @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2008, Brian Cully + */ + +#include "io.h" +#include "log.h" + +#include +#include +#include +#include + +int +restartable_read(unsigned char *buf, size_t buflen) +{ + ssize_t rc, readb; + + rc = 0; + READLOOP: + while (rc < buflen) { + readb = read(STDIN_FILENO, buf + rc, buflen - rc); + if (readb == -1) { + if (errno == EAGAIN || errno == EINTR) + goto READLOOP; + + return -1; + } else if (readb == 0) { + logmsg("ERROR: EOF trying to read additional %d bytes from " + "standard input", buflen - rc); + return -1; + } + + rc += readb; + } + + return rc; +} + +int +restartable_write(const unsigned char *buf, size_t buflen) +{ + ssize_t rc, wroteb; + + rc = 0; + WRITELOOP: + while (rc < buflen) { + wroteb = write(STDOUT_FILENO, buf + rc, buflen - rc); + if (wroteb == -1) { + if (errno == EAGAIN || errno == EINTR) + goto WRITELOOP; + + return -1; + } + + rc += wroteb; + } + + return rc; +} -- cgit v1.3