home *** CD-ROM | disk | FTP | other *** search
- /******************************************
- * CWRITE.C *
- * Copyright TimeSlice, Inc. 1985, 86, 87. *
- ******************************************/
-
- #include <ts.h>
-
- /**********
- * CWRITE( CHAN *CN , CHAR *BUF , INT NBYTES )
- * This function writes NBYTES from BUF to CN->Q->BUF. If NBYTES cannot be
- * transferred in one shot, it returns 0 else NBYTES.
- *
- * MODIFICATION
- *
- * 90-01-08 Bonné J.
- *
- * Added new parameter STATUS 0 CW_TOP regular write channel q->top
- * 1 CW_AHEAD write queue to q->ahead
- *
- **********/
-
- _cwrite( cn , status, buf , nbytes)
- CHAN *cn ;
- int status ;
- char *buf ;
- int nbytes ;
- {
- int a, b, t, cpuflags;
-
- cpuflags = cli() ; /* disable interrupts */
- if ( cn->wf && cn->wf != curproc ) { /* reserved for someone else*/
- putf( cpuflags ) ;
- return 0 ;
- }
-
- switch (status)
- {
- case CW_APPEND :
- case CW_TOP : t = cn->q.top; break;
- case CW_AHEAD : t = cn->q.ahead; break;
- }
-
- if( t >= cn->q.bot ) {
- a = cn->q.size - t;
- b = cn->q.bot ;
- } else {
- a = cn->q.bot - t;
- b = 0;
- }
-
- if ( ((a + b < nbytes) || cn->q.full) && (status != CW_APPEND) )
- { /* is channel full ? */
- putf( cpuflags ); /* restore cpu state */
- return 0;
- }
-
- memcpy( cn->q.buf + t , buf , nbytes > a ? a : nbytes );
- if( nbytes > a )
- memcpy( cn->q.buf , buf + a , nbytes - a );
- t = ( t + nbytes ) % cn->q.size;
-
- if (status == CW_AHEAD)
- {
- cn->q.ahead = t;
- }
- else
- {
- cn->q.empty = 0;
- cn->q.top = t;
- if (status == CW_TOP)
- cn->q.full = ( t == cn->q.bot );
-
- if (status == CW_APPEND && (a + b < nbytes))
- cn->q.bot = cn->q.top + 1;
- }
- putf( cpuflags ) ; /* restore cpu state */
- return nbytes ;
- }
-