home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / libncftp / older_versions / libncftp-3.1.5-src.tar.gz / libncftp-3.1.5-src.tar / libncftp-3.1.5 / sio / SWait.c < prev    next >
C/C++ Source or Header  |  2002-06-28  |  2KB  |  130 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6. /* 
  7.  * Return zero if the operation timed-out or erred-out, otherwise non-zero.
  8.  */
  9. int
  10. SWaitUntilReadyForReading(const int sfd, const int tlen)
  11. {
  12.     fd_set ss, ss2;
  13.     struct timeval tv;
  14.     int result;
  15.     int tleft;
  16.     time_t now, done;
  17.  
  18.     if (sfd < 0) {
  19.         errno = EBADF;
  20.         return (0);
  21.     }
  22.  
  23.     time(&now);
  24.     done = now + tlen;
  25.     tleft = tlen;
  26.  
  27.     forever {
  28.         MY_FD_ZERO(&ss);
  29. #if defined(__DECC) || defined(__DECCXX)
  30. #pragma message save
  31. #pragma message disable trunclongint
  32. #endif
  33.         MY_FD_SET(sfd, &ss);
  34. #if defined(__DECC) || defined(__DECCXX)
  35. #pragma message restore
  36. #endif
  37.         ss2 = ss;
  38.         tv.tv_sec = (tv_sec_t) tleft;
  39.         tv.tv_usec = 0;
  40.         result = select(sfd + 1, SELECT_TYPE_ARG234 &ss, NULL, SELECT_TYPE_ARG234 &ss2, &tv);
  41.         if (result == 1) {
  42.             /* ready */
  43.             return (1);
  44.         } else if (result < 0) {
  45.             if (errno != EINTR) {
  46.                 /* error */
  47.                 break;
  48.             }
  49.             /* try again */
  50.             time(&now);
  51.             if (now > done) {
  52.                 /* timed-out */
  53.                 errno = ETIMEDOUT;
  54.                 break;
  55.             }
  56.             tleft = (int) (done - now);
  57.         } else {
  58.             /* timed-out */
  59.             errno = ETIMEDOUT;
  60.             break;
  61.         }
  62.     }
  63.  
  64.     return (0);
  65. }    /* SWaitUntilReadyForReading */
  66.  
  67.  
  68.  
  69.  
  70. /* 
  71.  * Return zero if the operation timed-out or erred-out, otherwise non-zero.
  72.  */
  73. int
  74. SWaitUntilReadyForWriting(const int sfd, const int tlen)
  75. {
  76.     fd_set ss, ss2;
  77.     struct timeval tv;
  78.     int result;
  79.     int tleft;
  80.     time_t now, done;
  81.  
  82.     if (sfd < 0) {
  83.         errno = EBADF;
  84.         return (0);
  85.     }
  86.  
  87.     time(&now);
  88.     done = now + tlen;
  89.     tleft = tlen;
  90.  
  91.     forever {
  92.         MY_FD_ZERO(&ss);
  93. #if defined(__DECC) || defined(__DECCXX)
  94. #pragma message save
  95. #pragma message disable trunclongint
  96. #endif
  97.         MY_FD_SET(sfd, &ss);
  98. #if defined(__DECC) || defined(__DECCXX)
  99. #pragma message restore
  100. #endif
  101.         ss2 = ss;
  102.         tv.tv_sec = (tv_sec_t) tleft;
  103.         tv.tv_usec = 0;
  104.         result = select(sfd + 1, NULL, SELECT_TYPE_ARG234 &ss, SELECT_TYPE_ARG234 &ss2, &tv);
  105.         if (result == 1) {
  106.             /* ready */
  107.             return (1);
  108.         } else if (result < 0) {
  109.             if (errno != EINTR) {
  110.                 /* error */
  111.                 break;
  112.             }
  113.             /* try again */
  114.             time(&now);
  115.             if (now > done) {
  116.                 /* timed-out */
  117.                 errno = ETIMEDOUT;
  118.                 break;
  119.             }
  120.             tleft = (int) (done - now);
  121.         } else {
  122.             /* timed-out */
  123.             errno = ETIMEDOUT;
  124.             break;
  125.         }
  126.     }
  127.  
  128.     return (0);
  129. }    /* SWaitUntilReadyForWriting */
  130.