home *** CD-ROM | disk | FTP | other *** search
- /******************************************
- * CREAD.C *
- * Copyright TimeSlice, Inc. 1985, 86, 87. *
- ******************************************/
-
- #include <ts.h>
-
-
- /**********
- * CREAD( CHAN *CN , CHAR *BUF , INT NBYTES )
- * This function reads NBYTES from CN->Q->BUF to BUF. If NBYTES cannot be
- * transferred in one shot, it returns 0 else NBYTES.
- **********/
- _cread( cn , status, buf , nbytes )
- CHAN *cn ;
- int status ;
- char *buf ;
- int nbytes ;
- {
- int a, b, t, cpuflags;
-
- cpuflags = cli() ; /* disable interrupts */
- if ( cn->rf && cn->rf != curproc ) { /* reserved for someone else*/
- putf( cpuflags ) ;
- return 0 ;
- }
-
- t = (status == CW_BOT) ? cn->q.bot : cn->q.ahead;
-
- if( cn->q.top <= t ) {
- a = cn->q.size - t;
- b = cn->q.top ;
- } else {
- a = cn->q.top - t ;
- b = 0;
- }
-
- if (status == CW_BOT)
- {
- if ( (a + b < nbytes) || cn->q.empty ) /* is channel empty ? */
- {
- putf( cpuflags ) ;
- return 0;
- }
- }
- else /* Reading ahead, we can't rely on empty bit */
- {
- if ( (a + b < nbytes) || (cn->q.top == cn->q.ahead) )
- {
- putf( cpuflags ) ;
- return 0;
- }
- }
-
- memcpy( buf , cn->q.buf + t , nbytes > a ? a : nbytes );
- if( nbytes > a )
- memcpy( buf + a , cn->q.buf , nbytes - a );
- t = ( t + nbytes ) % cn->q.size;
-
- if (status == CW_AHEAD)
- {
- cn->q.ahead = t;
- }
- else
- {
- cn->q.empty = ((cn->q.bot = t) == cn->q.top );
- cn->q.full = 0;
- }
- putf( cpuflags ) ;
- return nbytes ;
-
- }
-