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 / USendto.c < prev    next >
C/C++ Source or Header  |  2009-12-17  |  2KB  |  78 lines

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