home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / s / s001 / 1.ddi / TS / SRC / CWRITE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-16  |  1.9 KB  |  79 lines

  1.                 /******************************************
  2.            *            CWRITE.C             *
  3.                * Copyright TimeSlice, Inc. 1985, 86, 87. *
  4.                ******************************************/
  5.  
  6. #include <ts.h>
  7.  
  8. /**********
  9. * CWRITE( CHAN *CN , CHAR *BUF , INT NBYTES ) 
  10. * This function writes NBYTES from BUF to CN->Q->BUF. If NBYTES cannot be
  11. * transferred in one shot, it returns 0 else NBYTES.
  12. *
  13. * MODIFICATION
  14. *
  15. * 90-01-08 BonnĂ© J.
  16. *
  17. * Added new parameter STATUS   0   CW_TOP      regular write channel q->top
  18. *                   1   CW_AHEAD    write queue to q->ahead
  19. *
  20. **********/
  21.  
  22. _cwrite( cn , status, buf , nbytes)
  23. CHAN    *cn ;
  24. int    status ;
  25. char    *buf ;
  26. int    nbytes ;
  27. {
  28.     int a, b, t, cpuflags;
  29.  
  30.     cpuflags = cli() ;                   /* disable interrupts */
  31.     if ( cn->wf && cn->wf != curproc ) {       /* reserved for someone else*/
  32.         putf( cpuflags ) ;
  33.         return 0 ;
  34.     }
  35.  
  36.     switch (status)
  37.     {
  38.       case CW_APPEND :
  39.       case CW_TOP    : t = cn->q.top;        break;
  40.       case CW_AHEAD  : t = cn->q.ahead;     break;
  41.     }
  42.  
  43.     if( t >= cn->q.bot ) {
  44.     a = cn->q.size - t;
  45.     b = cn->q.bot ;
  46.     } else {
  47.     a = cn->q.bot - t;
  48.     b = 0;
  49.     }
  50.  
  51.     if ( ((a + b < nbytes) || cn->q.full) && (status != CW_APPEND) )
  52.     {                            /* is channel full ? */
  53.     putf( cpuflags );                /* restore cpu state */
  54.     return 0;
  55.     }
  56.  
  57.     memcpy( cn->q.buf + t , buf , nbytes > a ? a : nbytes );
  58.     if( nbytes > a )
  59.     memcpy( cn->q.buf , buf + a , nbytes - a );
  60.     t = ( t + nbytes ) % cn->q.size;
  61.  
  62.     if (status == CW_AHEAD)
  63.     {
  64.        cn->q.ahead = t;
  65.     }
  66.     else
  67.     {
  68.        cn->q.empty = 0;
  69.        cn->q.top   = t;
  70.        if (status == CW_TOP)
  71.       cn->q.full  = ( t == cn->q.bot );
  72.  
  73.        if (status == CW_APPEND && (a + b < nbytes))
  74.       cn->q.bot = cn->q.top + 1;
  75.     }
  76.     putf( cpuflags ) ;                    /* restore cpu state */
  77.     return nbytes ;
  78. }
  79.