home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / s / s001 / 1.ddi / TS / SRC / CREAD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-23  |  1.6 KB  |  73 lines

  1.                 /******************************************
  2.            *            CREAD.C             *
  3.                * Copyright TimeSlice, Inc. 1985, 86, 87. *
  4.                ******************************************/
  5.  
  6. #include <ts.h>
  7.  
  8.  
  9. /**********
  10. * CREAD( CHAN *CN , CHAR *BUF , INT NBYTES ) 
  11. * This function reads NBYTES from CN->Q->BUF to BUF. If NBYTES cannot be
  12. * transferred in one shot, it returns 0 else NBYTES.
  13. **********/
  14. _cread( cn , status, buf , nbytes )
  15. CHAN    *cn ;
  16. int    status ;
  17. char    *buf ;
  18. int    nbytes ;
  19. {
  20.     int a, b, t, cpuflags;
  21.  
  22.     cpuflags = cli() ;                       /* disable interrupts */
  23.     if ( cn->rf && cn->rf != curproc ) {       /* reserved for someone else*/
  24.         putf( cpuflags ) ;
  25.         return 0 ;
  26.     }
  27.  
  28.     t = (status == CW_BOT) ? cn->q.bot : cn->q.ahead;
  29.  
  30.     if( cn->q.top <= t ) {
  31.     a = cn->q.size - t;
  32.     b = cn->q.top ;
  33.     } else {
  34.     a = cn->q.top - t ;
  35.     b = 0;
  36.     }
  37.  
  38.     if (status == CW_BOT)
  39.     {
  40.        if ( (a + b < nbytes) || cn->q.empty )    /* is channel empty ? */
  41.        {
  42.       putf( cpuflags ) ;
  43.       return 0;
  44.        }
  45.     }
  46.     else   /* Reading ahead, we can't rely on empty bit */
  47.     {
  48.        if ( (a + b < nbytes) || (cn->q.top == cn->q.ahead) )
  49.        {
  50.       putf( cpuflags ) ;
  51.       return 0;
  52.        }
  53.     }
  54.  
  55.     memcpy( buf , cn->q.buf + t , nbytes > a ? a : nbytes );
  56.     if( nbytes > a )
  57.     memcpy( buf + a , cn->q.buf , nbytes - a );
  58.     t = ( t + nbytes ) % cn->q.size;
  59.  
  60.     if (status == CW_AHEAD)
  61.     {
  62.        cn->q.ahead = t;
  63.     }
  64.     else
  65.     {
  66.        cn->q.empty = ((cn->q.bot = t) == cn->q.top );
  67.        cn->q.full  = 0;
  68.     }
  69.     putf( cpuflags ) ;
  70.     return nbytes ;
  71.  
  72. }
  73.