Handle errors from write()

Fixes #964
This commit is contained in:
ZyX 2014-08-06 22:04:15 +04:00
parent 0853f60391
commit ae2ac05ced
1 changed files with 24 additions and 1 deletions

View File

@ -120,7 +120,30 @@ int main(int argc, char *argv[]) {
close(sd);
HANDLE_ERROR("read() failed");
} else if (i > 0) {
(void) write(STDOUT_FILENO, buf, (size_t) i);
ptrdiff_t written = 0;
const char *bufp = &(buf[0]);
while (written < i) {
written = write(STDOUT_FILENO, buf, (size_t) i);
if (written >= 0) {
bufp += written;
i -= written;
written = 0;
} else {
errno = 0;
switch (errno) {
case EAGAIN:
#if EAGAIN != EWOULDBLOCK
case EWOULDBLOCK:
#endif
case EINTR:
break;
default:
(void) printf("Error while writing output: %s", strerror(errno));
close(sd);
return 2;
}
}
}
}
}