home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / misc / src / rpm / lib / tread.c < prev    next >
C/C++ Source or Header  |  1997-09-17  |  702b  |  39 lines

  1. #include "miscfn.h"
  2.  
  3. #include <string.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7.  
  8. #include "tread.h"
  9.  
  10. int timedRead(int fd, void * bufptr, int length) {
  11.     int bytesRead;
  12.     int total = 0;
  13.     char * buf = bufptr;
  14.     fd_set readSet;
  15.     struct timeval tv;
  16.  
  17.     while  (total < length) {
  18.     FD_ZERO(&readSet);
  19.     FD_SET(fd, &readSet);
  20.  
  21.     tv.tv_sec = 5;            /* FIXME: this should be configurable */
  22.     tv.tv_usec = 0;
  23.  
  24.     if (select(fd + 1, &readSet, NULL, NULL, &tv) != 1) 
  25.         return total;
  26.  
  27.     bytesRead = read(fd, buf + total, length - total);
  28.  
  29.     if (bytesRead < 0)
  30.         return bytesRead;
  31.     else if (bytesRead == 0) 
  32.         return total;
  33.  
  34.     total += bytesRead;
  35.     }
  36.  
  37.     return length;
  38. }
  39.