home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / libncftp / libncftp-3.2.5-src.zip / libncftp-3.2.5 / sio / PRead.c < prev    next >
C/C++ Source or Header  |  2003-08-31  |  1KB  |  62 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6. /* Read up to "size" bytes on sfd.
  7.  *
  8.  * If "retry" is on, after a successful read of less than "size"
  9.  * bytes, it will attempt to read more, upto "size."
  10.  *
  11.  * Although "retry" would seem to indicate you may want to always
  12.  * read "size" bytes or else it is an error, even with that on you
  13.  * may get back a value < size.  Set "retry" to 0 when you want to
  14.  * return as soon as there is a chunk of data whose size is <= "size".
  15.  */
  16.  
  17. int
  18. PRead(int sfd, char *const buf0, size_t size, int retry)
  19. {
  20.     read_return_t nread;
  21.     read_size_t nleft;
  22.     char *buf = buf0;
  23.     DECL_SIGPIPE_VARS
  24.     
  25.     if ((buf == NULL) || (size == 0)) {
  26.         errno = EINVAL;
  27.         return (-1);
  28.     }
  29.     
  30.     IGNORE_SIGPIPE
  31.     errno = 0;
  32.     nleft = (read_size_t) size;
  33.     forever {
  34.         nread = read(sfd, buf, nleft);
  35.         if (nread <= 0) {
  36.             if (nread == 0) {
  37.                 /* EOF */
  38.                 nread = (read_return_t) size - (read_return_t) nleft;
  39.                 goto done;
  40.             } else if (errno != EINTR) {
  41.                 nread = (read_return_t) size - (read_return_t) nleft;
  42.                 if (nread == 0)
  43.                     nread = (read_return_t) -1;
  44.                 goto done;
  45.             } else {
  46.                 errno = 0;
  47.                 nread = 0;
  48.                 /* Try again. */
  49.             }
  50.         }
  51.         nleft -= (read_size_t) nread;
  52.         if ((nleft == 0) || (retry == 0))
  53.             break;
  54.         buf += nread;
  55.     }
  56.     nread = (read_return_t) size - (read_return_t) nleft;
  57.  
  58. done:
  59.     RESTORE_SIGPIPE
  60.     return ((int) nread);
  61. }    /* PRead */
  62.