home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdio / fflush.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-22  |  903 b   |  43 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <libc/file.h>
  10. #include <io.h>
  11.  
  12. int
  13. fflush(FILE *f)
  14. {
  15.   char *base;
  16.   int n, rn;
  17.  
  18.   f->_flag &= ~_IOUNGETC;
  19.   if ((f->_flag&(_IONBF|_IOWRT))==_IOWRT
  20.       && (base = f->_base) != NULL
  21.       && (rn = n = f->_ptr - base) > 0)
  22.   {
  23.     f->_ptr = base;
  24.     f->_cnt = (f->_flag&(_IOLBF|_IONBF)) ? 0 : f->_bufsiz;
  25.     do {
  26.       n = _write(fileno(f), base, rn);
  27.       if (n <= 0) {
  28.     f->_flag |= _IOERR;
  29.     return EOF;
  30.       }
  31.       rn -= n;
  32.       base += n;
  33.     } while (rn > 0);
  34.   }
  35.   if (f->_flag & _IORW)
  36.   {
  37.     f->_cnt = 0;
  38.     f->_flag &= ~(_IOWRT|_IOREAD);
  39.     f->_ptr = f->_base;
  40.   }
  41.   return 0;
  42. }
  43.