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

  1. /*------------------------------------------------------------------------*
  2.  * filename - fflush.c
  3.  *
  4.  * function(s)
  5.  *        fflush - flushes a stream
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #include <stdio.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 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.