improve readfd and writefd (blocking) - sob - simple output bar | |
git clone git://git.codemadness.org/sob | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 7c32499ad075426acaa9f44813059b630aa52a12 | |
parent fe1be9488f907f205711eda02c47a5b9aab08fd2 | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Sun, 26 Oct 2014 14:35:59 +0000 | |
improve readfd and writefd (blocking) | |
Diffstat: | |
M sob.c | 55 ++++++++++++++++++++++-------… | |
1 file changed, 39 insertions(+), 16 deletions(-) | |
--- | |
diff --git a/sob.c b/sob.c | |
@@ -558,7 +558,9 @@ readfd(int fd, char *buf, size_t len) { | |
if(errno == EINTR) | |
continue; | |
if(errno == EWOULDBLOCK || errno == EAGAIN) | |
- return i; | |
+ return -1; | |
+ fprintf(stderr, "read: %s\n", strerror(errno)); | |
+ fflush(stderr); | |
return -1; | |
} else if(r == 0) { | |
return i; | |
@@ -577,8 +579,12 @@ writefd(int fd, char *buf, size_t len) { | |
if((w = write(fd, &buf[i], len)) == -1) { | |
if(errno == EINTR) | |
continue; | |
- if(errno == EWOULDBLOCK || errno == EAGAIN) | |
+ if(errno == EPIPE) | |
return i; | |
+ if(errno == EWOULDBLOCK || errno == EAGAIN) | |
+ continue; | |
+ fprintf(stderr, "write: %s\n", strerror(errno)); | |
+ fflush(stderr); | |
return -1; | |
} else if(w == 0) { | |
return i; | |
@@ -615,15 +621,27 @@ pipe_read(int fd_in, int fd_out, char *writestr, | |
tv.tv_usec = 50000; /* 50 ms */ | |
if((r = select(maxfd + 1, haswritten ? &fdr : NULL, | |
- haswritten ? NULL : &fdw, NULL, &tv)) == -1) | |
- goto fini; | |
- else if(!r) /* timeout */ | |
+ haswritten ? NULL : &fdw, NULL, &tv)) == -1… | |
+ if(errno != EINTR) | |
+ goto fini; | |
+ } else if(!r) { /* timeout */ | |
continue; | |
- | |
+ } | |
+ if(FD_ISSET(fd_out, &fdw)) { | |
+ if(writefd(fd_out, writestr, strlen(writestr)) == -1) | |
+ goto fini; | |
+ close(fd_out); /* sends EOF */ | |
+ fd_out = -1; | |
+ haswritten = 1; | |
+ } | |
if(FD_ISSET(fd_in, &fdr) && haswritten) { | |
while(1) { | |
- if((r = readfd(fd_in, buf, sizeof(buf))) == -1) | |
+ r = readfd(fd_in, buf, sizeof(buf)); | |
+ if(r == -1) { | |
+ if(errno == EWOULDBLOCK || errno == EA… | |
+ continue; | |
goto fini; | |
+ } | |
if(!r) { | |
status = 0; | |
goto fini; | |
@@ -635,13 +653,6 @@ pipe_read(int fd_in, int fd_out, char *writestr, | |
} | |
} | |
} | |
- if(FD_ISSET(fd_out, &fdw)) { | |
- if(writefd(fd_out, writestr, strlen(writestr)) == -1) | |
- goto fini; | |
- close(fd_out); /* sends EOF */ | |
- fd_out = -1; | |
- haswritten = 1; | |
- } | |
} | |
fini: | |
if(fd_in != -1) | |
@@ -872,8 +883,12 @@ run(void) | |
continue; /* timeout */ | |
} | |
if(FD_ISSET(STDIN_FILENO, &fdr)) { | |
- if((r = readfd(STDIN_FILENO, (char *)buf, sizeof(buf))… | |
+ r = readfd(STDIN_FILENO, (char *)buf, sizeof(buf)); | |
+ if(r == -1) { | |
+ if(errno == EWOULDBLOCK || errno == EAGAIN) | |
+ continue; | |
goto fini; | |
+ } | |
if(r > 0) { | |
buf[r] = '\0'; | |
handleinput(buf, r); | |
@@ -912,7 +927,13 @@ initialinput(void) | |
/* read initial input from stdin */ | |
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); | |
while(1) { | |
- if((r = readfd(STDIN_FILENO, (char *)buf, sizeof(buf))) <= 0) | |
+ r = readfd(STDIN_FILENO, (char *)buf, sizeof(buf)); | |
+ if(r == -1) { | |
+ if(errno == EWOULDBLOCK || errno == EAGAIN) | |
+ continue; | |
+ break; | |
+ } | |
+ if(r == 0) | |
break; | |
buf[r] = '\0'; | |
handleinput(buf, r); | |
@@ -961,6 +982,8 @@ main(int argc, char **argv) | |
/* reap zombie childs >=) */ | |
sa.sa_handler = SIG_IGN; | |
sigaction(SIGCHLD, &sa, NULL); | |
+ /* ignore SIGPIPE, we handle this for write(). */ | |
+ sigaction(SIGPIPE, &sa, NULL); | |
setup(); | |