fixed a select bug which caused rsync to use far more cpu time than
[rsync/rsync.git] / io.c
diff --git a/io.c b/io.c
index f5816ed..52c4cb1 100644 (file)
--- a/io.c
+++ b/io.c
@@ -148,7 +148,8 @@ static int readfd(int fd,char *buffer,int N)
                        tv.tv_sec = io_timeout;
                        tv.tv_usec = 0;
 
-                       if (select(fd+1, &fds, NULL, NULL, &tv) != 1) {
+                       if (select(fd+1, &fds, NULL, NULL, 
+                                  io_timeout?&tv:NULL) != 1) {
                                check_timeout();
                        }
                }
@@ -423,10 +424,16 @@ void write_buf(int f,char *buf,int len)
   total_written += len;
 }
 
+/* write a string to the connection */
+void write_sbuf(int f,char *buf)
+{
+       write_buf(f, buf, strlen(buf));
+}
+
 
 void write_byte(int f,unsigned char c)
 {
-  write_buf(f,(char *)&c,1);
+       write_buf(f,(char *)&c,1);
 }
 
 void write_flush(int f)
@@ -434,3 +441,44 @@ void write_flush(int f)
 }
 
 
+int read_line(int f, char *buf, int maxlen)
+{
+       while (maxlen) {
+               read_buf(f, buf, 1);
+               if (buf[0] == '\n') {
+                       buf[0] = 0;
+                       break;
+               }
+               if (buf[0] != '\r') {
+                       buf++;
+                       maxlen--;
+               }
+       }
+       if (maxlen == 0) {
+               *buf = 0;
+               return 0;
+       }
+       return 1;
+}
+
+
+void io_printf(int fd, const char *format, ...)
+{
+       va_list ap;  
+       char buf[1024];
+       int len;
+       
+       va_start(ap, format);
+
+#if HAVE_VSNPRINTF
+       len = vsnprintf(buf, sizeof(buf)-1, format, ap);
+#else
+       vsprintf(buf, format, ap);
+       len = strlen(buf);
+#endif
+       va_end(ap);
+
+       if (len < 0) exit_cleanup(1);
+
+       write_sbuf(fd, buf);
+}