chiark / gitweb /
changelog: finalise 5.0.1~citrix1
[chiark-utils.git] / cprogs / wrbufcore.c
index 417cb01ac0cef44f7cc03c80f04f0120152fc857..65fc2bede367227631169004b5e2598b84aa44fc 100644 (file)
@@ -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
  * 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 fdsetset(int fd, fd_set *set) {
+  FD_SET(fd,set);
+  if (fd >= maxselfd) maxselfd= fd+1;
 }
 
 void wrbufcore_prepselect(int rdfd, int wrfd) {
   FD_ZERO(&readfds);
-  if (rdfd>=0 && !seeneof && used+1<buffersize) FD_SET(rdfd,&readfds);
-
+  if (rdfd>=0 && !seeneof && used+1<buffersize) fdsetset(rdfd,&readfds);
+  
   FD_ZERO(&writefds);
-  if (writing) FD_SET(wrfd,&writefds);
+  if (writing) fdsetset(wrfd,&writefds);
 }
 
 void wrbufcore_afterselect(int rdfd, int wrfd) {
@@ -51,6 +59,7 @@ void wrbufcore_afterselect(int rdfd, int wrfd) {
   if (FD_ISSET(wrfd,&writefds) &&
       !(rdfd>=0 && FD_ISSET(rdfd,&readfds)) &&
       !used) {
+    wrbuf_report("stopping");
     writing= 0;
     FD_CLR(wrfd,&writefds);
   }
@@ -59,6 +68,7 @@ void wrbufcore_afterselect(int rdfd, int wrfd) {
     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 {
@@ -66,7 +76,10 @@ void wrbufcore_afterselect(int rdfd, int wrfd) {
       rp+= r;
       if (rp == buf+buffersize) rp=buf;
     }
-    if (used > waitfill) writing=1;
+    if (used > waitfill) {
+      if (!writing) wrbuf_report("starting");
+      writing=1;
+    }
   }
 
   if (FD_ISSET(wrfd,&writefds) && used) {