home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftp / older_versions / ncftp-3.2.2-src.tar.bz2 / ncftp-3.2.2-src.tar / ncftp-3.2.2 / sio / SSendto.c < prev    next >
C/C++ Source or Header  |  2003-08-31  |  2KB  |  99 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6. int
  7. SSendto(int sfd, const char *const buf, size_t size, int fl, const struct sockaddr_in *const toAddr, int tlen)
  8. {
  9.     send_return_t nwrote;
  10.     int tleft;
  11.     time_t done, now;
  12.     fd_set ss;
  13.     struct timeval tv;
  14.     int result;
  15.     DECL_SIGPIPE_VARS
  16.     
  17.     if ((buf == NULL) || (size == 0) || (toAddr == NULL) || (tlen <= 0)) {
  18.         errno = EINVAL;
  19.         return (-1);
  20.     }
  21.     
  22.     time(&now);
  23.     done = now + tlen;
  24.     nwrote = 0;
  25.     forever {
  26.         forever {
  27.             if (now >= done) {
  28.                 errno = ETIMEDOUT;
  29.                 SETWSATIMEOUTERR
  30.                 return (kTimeoutErr);
  31.             }
  32.             tleft = (done > now) ? ((int) (done - now)) : 0;
  33.             errno = 0;
  34.             MY_FD_ZERO(&ss);
  35. #if defined(__DECC) || defined(__DECCXX)
  36. #pragma message save
  37. #pragma message disable trunclongint
  38. #endif
  39.             MY_FD_SET(sfd, &ss);
  40. #if defined(__DECC) || defined(__DECCXX)
  41. #pragma message restore
  42. #endif
  43.             tv.tv_sec = (tv_sec_t) tleft;
  44.             tv.tv_usec = 0;
  45.             result = select(sfd + 1, NULL, SELECT_TYPE_ARG234 &ss, NULL, SELECT_TYPE_ARG5 &tv);
  46.             if (result == 1) {
  47.                 /* ready */
  48.                 break;
  49.             } else if (result == 0) {
  50.                 /* timeout */        
  51.                 errno = ETIMEDOUT;
  52.                 SETWSATIMEOUTERR
  53.                 return (kTimeoutErr);
  54.             } else if (errno != EINTR) {
  55.                 return (-1);
  56.             }
  57.             time(&now);
  58.         }
  59.  
  60.         IGNORE_SIGPIPE
  61.         nwrote = sendto(sfd, buf, (send_size_t) size, fl,
  62.             (const struct sockaddr *) toAddr,
  63.             (sockaddr_size_t) sizeof(struct sockaddr_in));
  64.         RESTORE_SIGPIPE
  65.  
  66.         if (nwrote >= 0)
  67.             break;
  68.         if (errno != EINTR)
  69.             break;        /* Fatal error. */
  70.     }
  71.  
  72.     return ((int) nwrote);
  73. }    /* SSendto */
  74.  
  75.  
  76.  
  77.  
  78.  
  79. int
  80. Sendto(int sfd, const char *const buf, size_t size, const struct sockaddr_in *const toAddr)
  81. {
  82.     int result;
  83.     DECL_SIGPIPE_VARS
  84.     
  85.     if ((buf == NULL) || (size == 0) || (toAddr == NULL)) {
  86.         errno = EINVAL;
  87.         return (-1);
  88.     }
  89.     
  90.     IGNORE_SIGPIPE
  91.     do {
  92.         result = (int) sendto(sfd, buf, (send_size_t) size, 0,
  93.                 (const struct sockaddr *) toAddr,
  94.                 (sockaddr_size_t) sizeof(struct sockaddr_in));
  95.     } while ((result < 0) && (errno == EINTR));
  96.     RESTORE_SIGPIPE
  97.     return (result);
  98. }    /* Sendto */
  99.