Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. int pr_netio_read(pr_netio_stream_t *nstrm, char *buf, size_t buflen,
  2.     int bufmin) {
  3.   int bread = 0, total = 0;
  4.  
  5.   /* Sanity check. */
  6.   if (!nstrm) {
  7.     errno = EINVAL;
  8.     return -1;
  9.   }
  10.  
  11.   if (nstrm->strm_fd == -1) {
  12.     errno = (nstrm->strm_errno ? nstrm->strm_errno : EBADF);
  13.     return -1;
  14.   }
  15.  
  16.   if (bufmin < 1)
  17.     bufmin = 1;
  18.  
  19.   if (bufmin > buflen)
  20.     bufmin = buflen;
  21.  
  22.   while (bufmin > 0) {
  23.     polling:
  24.     switch (pr_netio_poll(nstrm)) {
  25.       case 1:
  26.         return -2;
  27.  
  28.       case -1:
  29.         return -1;
  30.  
  31.       default:
  32.         do {
  33.           pr_signals_handle();
  34.           run_schedule();
  35.  
  36.           switch (nstrm->strm_type) {
  37.             case PR_NETIO_STRM_CTRL:
  38.               bread = ctrl_netio ? ctrl_netio->read(nstrm, buf, buflen) :
  39.                 core_ctrl_netio->read(nstrm, buf, buflen);
  40.                 break;
  41.  
  42.             case PR_NETIO_STRM_DATA:
  43.               bread = data_netio ? data_netio->read(nstrm, buf, buflen) :
  44.                 core_data_netio->read(nstrm, buf, buflen);
  45.               break;
  46.  
  47.             case PR_NETIO_STRM_OTHR:
  48.               bread = othr_netio ? othr_netio->read(nstrm, buf, buflen) :
  49.                 core_othr_netio->read(nstrm, buf, buflen);
  50.               break;
  51.           }
  52.  
  53. #ifdef EAGAIN
  54.       if (bread == -1 && errno == EAGAIN)
  55.             goto polling;
  56. #endif
  57.  
  58.         } while (bread == -1 && errno == EINTR);
  59.         break;
  60.     }
  61.  
  62.     if (bread == -1) {
  63.       nstrm->strm_errno = errno;
  64.       return -1;
  65.     }
  66.  
  67.     /* EOF? */
  68.     if (bread == 0) {
  69.       nstrm->strm_errno = 0;
  70.       break;
  71.     }
  72.  
  73.     buf += bread;
  74.     total += bread;
  75.     bufmin -= bread;
  76.     buflen -= bread;
  77.   }
  78.  
  79.   return total;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement