X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=blobdiff_plain;f=cprogs%2Fwrbufcore.c;h=65fc2bede367227631169004b5e2598b84aa44fc;hp=260f6d2c75f37a5e9031ba34d31c8f0f5e8db891;hb=1e17ba9014e80fc58acfa88b20e8fd2744d994d5;hpb=737b84851cd8b3dd1d9f1920201127eac693dffb diff --git a/cprogs/wrbufcore.c b/cprogs/wrbufcore.c index 260f6d2..65fc2be 100644 --- a/cprogs/wrbufcore.c +++ b/cprogs/wrbufcore.c @@ -13,7 +13,7 @@ * * This is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2, + * published by the Free Software Foundation; either version 3, * or (at your option) any later version. * * This is distributed in the hope that it will be useful, but @@ -22,57 +22,74 @@ * GNU General Public License for more details. * * You should have received a copy of the GNU General Public - * License along with this file; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * License along with this file; if not, consult the Free Software + * Foundation's website at www.fsf.org, or the GNU Project website at + * www.gnu.org. * */ +#include "rwbuffer.h" + static size_t waitfill; -static int writing; + +int writing; void wrbufcore_startup(void) { waitfill= (buffersize*3)/4; writing=0; + maxselfd=0; } -void wrbufcore_mainloop(void) { - int r; +void fdsetset(int fd, fd_set *set) { + FD_SET(fd,set); + if (fd >= maxselfd) maxselfd= fd+1; +} - while (!seeneof || used) { - - FD_ZERO(&readfds); if (!seeneof && used+1=0 && !seeneof && used+1=0 && FD_ISSET(rdfd,&readfds)) && + !used) { + wrbuf_report("stopping"); + writing= 0; + FD_CLR(wrfd,&writefds); + } - if (FD_ISSET(0,&readfds)) { - r= read(0,rp,min(buffersize-1-used,buf+buffersize-rp)); - if (!r) { - seeneof=1; writing=1; - } else if (r<0) { - if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); } - } else { - used+= r; - rp+= r; - if (rp == buf+buffersize) rp=buf; - } - if (used > waitfill) writing=1; + if (rdfd>=0 && FD_ISSET(rdfd,&readfds)) { + r= read(rdfd,rp,min(buffersize-1-used,buf+buffersize-rp)); + if (!r) { + seeneof=1; writing=1; + wrbuf_report("seeneof"); + } else if (r<0) { + if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); } + } else { + used+= r; + rp+= r; + if (rp == buf+buffersize) rp=buf; } + if (used > waitfill) { + if (!writing) wrbuf_report("starting"); + writing=1; + } + } - if (FD_ISSET(1,&writefds) && used) { - r= write(1,wp,min(used,buf+buffersize-wp)); - if (r<=0) { - if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); } - } else { - used-= r; - wp+= r; - if (wp == buf+buffersize) wp=buf; - } + if (FD_ISSET(wrfd,&writefds) && used) { + r= write(wrfd,wp,min(used,buf+buffersize-wp)); + if (r<=0) { + if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); } + } else { + used-= r; + wp+= r; + if (wp == buf+buffersize) wp=buf; } } }