PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/ncftp-3.2.5/sio/SRecvmsg.c

#
C | 102 lines | 88 code | 12 blank | 2 comment | 21 complexity | 86424e395425f6e1a464c3d9a4d939b0 MD5 | raw file
Possible License(s): AGPL-3.0
  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. # pragma hdrstop
  4. #endif
  5. #ifndef HAVE_RECVMSG
  6. int
  7. SRecvmsg(int UNUSED(sfd), void *const UNUSED(msg), int UNUSED(fl), int UNUSED(tlen))
  8. {
  9. LIBSIO_USE_VAR(sfd);
  10. LIBSIO_USE_VAR(msg);
  11. LIBSIO_USE_VAR(fl);
  12. LIBSIO_USE_VAR(tlen);
  13. # ifdef ENOSYS
  14. errno = ENOSYS;
  15. # endif
  16. return (-1);
  17. }
  18. #else
  19. int
  20. SRecvmsg(int sfd, void *const msg, int fl, int tlen)
  21. {
  22. recv_return_t nread;
  23. int tleft;
  24. time_t done, now;
  25. fd_set ss;
  26. struct timeval tv;
  27. int result;
  28. DECL_SIGPIPE_VARS
  29. if (msg == NULL) {
  30. errno = EINVAL;
  31. return (-1);
  32. }
  33. if (tlen <= 0) {
  34. errno = 0;
  35. for (;;) {
  36. nread = recvmsg(sfd, (struct msghdr *) msg, fl);
  37. if ((nread >= 0) || (errno != EINTR))
  38. return ((int) nread);
  39. }
  40. }
  41. time(&now);
  42. done = now + tlen;
  43. tleft = (done > now) ? ((int) (done - now)) : 0;
  44. forever {
  45. for (;;) {
  46. errno = 0;
  47. MY_FD_ZERO(&ss);
  48. #if defined(__DECC) || defined(__DECCXX)
  49. #pragma message save
  50. #pragma message disable trunclongint
  51. #endif
  52. MY_FD_SET(sfd, &ss);
  53. #if defined(__DECC) || defined(__DECCXX)
  54. #pragma message restore
  55. #endif
  56. tv.tv_sec = (tv_sec_t) tleft;
  57. tv.tv_usec = 0;
  58. result = select(sfd + 1, SELECT_TYPE_ARG234 &ss, NULL, NULL, SELECT_TYPE_ARG5 &tv);
  59. if (result >= 1) {
  60. /* ready */
  61. break;
  62. } else if (result == 0) {
  63. /* timeout */
  64. errno = ETIMEDOUT;
  65. SETWSATIMEOUTERR
  66. return (kTimeoutErr);
  67. } else if (errno != EINTR) {
  68. return (-1);
  69. }
  70. }
  71. IGNORE_SIGPIPE
  72. nread = recvmsg(sfd, (struct msghdr *) msg, fl);
  73. RESTORE_SIGPIPE
  74. if (nread >= 0)
  75. break;
  76. if (errno != EINTR)
  77. break; /* Fatal error. */
  78. errno = 0;
  79. time(&now);
  80. tleft = (done > now) ? ((int) (done - now)) : 0;
  81. if (tleft < 1) {
  82. nread = kTimeoutErr;
  83. errno = ETIMEDOUT;
  84. SETWSATIMEOUTERR
  85. break;
  86. }
  87. }
  88. return ((int) nread);
  89. } /* SRecvmsg */
  90. #endif