home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 1.ddi / CLIBSRC1.ZIP / FFLUSH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  1.9 KB  |  76 lines

  1. /*------------------------------------------------------------------------*
  2.  * filename - fflush.c
  3.  *
  4.  * function(s)
  5.  *        fflush - flushes a stream
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*
  9.  *      C/C++ Run Time Library - Version 5.0
  10.  *
  11.  *      Copyright (c) 1987, 1992 by Borland International
  12.  *      All Rights Reserved.
  13.  *
  14.  */
  15.  
  16.  
  17. #include <stdio.h>
  18. #include <io.h>
  19. #include <_io.h>
  20.  
  21. /*------------------------------------------------------------------------*
  22.  
  23. Name            fflush - flushes a stream
  24.  
  25. Usage           #include <stdio.h>
  26.                 int fflush(FILE *stream);
  27.  
  28. Prototype in    stdio.h
  29.  
  30. Description     fflush causes the contents of the buffer associated with
  31.                 an open output stream to be written to stream, and clears
  32.                 the buffer contents if stream is an open input stream.
  33.                 stream remains open.
  34.  
  35. Return value    success : 0
  36.                 failure : EOF
  37.  
  38. *------------------------------------------------------------------------*/
  39.  
  40. int _FARFUNC fflush( register FILE *fp )
  41.   {
  42.   register count;
  43.  
  44.   if( !fp )
  45.     {
  46.     flushall();
  47.     }
  48.   else
  49.     {
  50.     if( fp->token != (short)fp )  return( EOF );      /* validity check */
  51.  
  52.     if( fp->level >= 0 )                    /* no output data in buffer */
  53.       {
  54.       if( fp->flags & _F_LBUF || fp->curp == &fp->hold )
  55.         {
  56.         fp->level = 0;                          /* ensure no unget char */
  57.         if( fp->curp == &fp->hold )  fp->curp = fp->buffer;
  58.         }
  59.  
  60.       return( 0 );
  61.       }
  62.  
  63.     count = fp->bsize + 1 + fp->level;
  64.     fp->level -= count;
  65.  
  66.     if( (__write( fp->fd, (fp->curp = fp->buffer), count ) != count) &&
  67.       ((fp->flags & _F_TERM) == 0) )
  68.       {
  69.       fp->flags |= _F_ERR;
  70.       return( EOF );
  71.       }
  72.     }
  73.  
  74.   return( 0 );
  75.   }
  76.