fflush.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * fflush.c - flush stream(s)
  3. */
  4. /* $Id$ */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include "loc_incl.h"
  9. int
  10. fflush(FILE *stream)
  11. {
  12. int count, c1, i, retval = 0;
  13. if (!stream) {
  14. for(i= 0; i < FOPEN_MAX; i++)
  15. if (__iotab[i] && fflush(__iotab[i]))
  16. retval = EOF;
  17. return retval;
  18. }
  19. if (!stream->_buf
  20. || (!io_testflag(stream, _IOREADING)
  21. && !io_testflag(stream, _IOWRITING)))
  22. return 0;
  23. if (io_testflag(stream, _IOREADING)) {
  24. /* (void) fseek(stream, 0L, SEEK_CUR); */
  25. int adjust = 0;
  26. if (stream->_buf && !io_testflag(stream,_IONBF))
  27. adjust = stream->_count;
  28. stream->_count = 0;
  29. lseek(fileno(stream), (off_t) adjust, SEEK_CUR);
  30. if (io_testflag(stream, _IOWRITE))
  31. stream->_flags &= ~(_IOREADING | _IOWRITING);
  32. stream->_ptr = stream->_buf;
  33. return 0;
  34. } else if (io_testflag(stream, _IONBF)) return 0;
  35. if (io_testflag(stream, _IOREAD)) /* "a" or "+" mode */
  36. stream->_flags &= ~_IOWRITING;
  37. count = stream->_ptr - stream->_buf;
  38. stream->_ptr = stream->_buf;
  39. if ( count <= 0 )
  40. return 0;
  41. if (io_testflag(stream, _IOAPPEND)) {
  42. if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
  43. stream->_flags |= _IOERR;
  44. return EOF;
  45. }
  46. }
  47. c1 = write(stream->_fd, (char *)stream->_buf, count);
  48. stream->_count = 0;
  49. if ( count == c1 )
  50. return 0;
  51. stream->_flags |= _IOERR;
  52. return EOF;
  53. }
  54. void
  55. __cleanup(void)
  56. {
  57. register int i;
  58. for(i= 0; i < FOPEN_MAX; i++)
  59. if (__iotab[i] && io_testflag(__iotab[i], _IOWRITING))
  60. (void) fflush(__iotab[i]);
  61. }