fflush.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * fflush.c - flush stream(s)
  3. */
  4. /* $Header$ */
  5. #include <stdio.h>
  6. #include "loc_incl.h"
  7. int write(int d, const char *buf, int nbytes);
  8. int
  9. fflush(FILE *stream)
  10. {
  11. int count, c1, i, retval = 0;
  12. if (!stream) {
  13. for(i= 0; i < FOPEN_MAX; i++)
  14. if (__iotab[i] && fflush(__iotab[i]))
  15. retval = EOF;
  16. return retval;
  17. }
  18. if (!stream->_buf
  19. || io_testflag(stream, _IONBF)
  20. || !io_testflag(stream, _IOWRITE)
  21. || !io_testflag(stream, _IOWRITING))
  22. return 0;
  23. if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
  24. stream->_flags &= ~_IOWRITING;
  25. /*
  26. if (io_testflag(stream, _IOLBF))
  27. count = -stream->_count;
  28. else count = stream->_bufsiz - stream->_count;
  29. */
  30. count = stream->_ptr - stream->_buf;
  31. stream->_ptr = stream->_buf;
  32. if ( count <= 0 )
  33. return 0;
  34. c1 = write(stream->_fd, (char *)stream->_buf, count);
  35. stream->_count = 0;
  36. /*
  37. if(stream != stderr)
  38. fprintf(stderr,"written %d bytes :\"%.*s\"\n"
  39. , c1, c1, stream->_buf);
  40. */
  41. if ( count == c1 )
  42. return 0;
  43. stream->_flags |= _IOERR;
  44. return EOF;
  45. }