fflush.c 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "stdio_impl.h"
  2. static int __fflush_unlocked(FILE *f)
  3. {
  4. /* If writing, flush output */
  5. if (f->wpos > f->wbase) {
  6. f->write(f, 0, 0);
  7. if (!f->wpos) return EOF;
  8. }
  9. /* If reading, sync position, per POSIX */
  10. if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
  11. /* Clear read and write modes */
  12. f->wpos = f->wbase = f->wend = 0;
  13. f->rpos = f->rend = 0;
  14. /* Hook for special behavior on flush */
  15. if (f->flush) f->flush(f);
  16. return 0;
  17. }
  18. /* stdout.c will override this if linked */
  19. static FILE *const dummy = 0;
  20. weak_alias(dummy, __stdout_used);
  21. int fflush(FILE *f)
  22. {
  23. int r;
  24. FILE *next;
  25. if (f) {
  26. FLOCK(f);
  27. r = __fflush_unlocked(f);
  28. FUNLOCK(f);
  29. return r;
  30. }
  31. r = __stdout_used ? fflush(__stdout_used) : 0;
  32. OFLLOCK();
  33. for (f=libc.ofl_head; f; f=next) {
  34. FLOCK(f);
  35. //OFLUNLOCK();
  36. if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
  37. //OFLLOCK();
  38. next = f->next;
  39. FUNLOCK(f);
  40. }
  41. OFLUNLOCK();
  42. return r;
  43. }
  44. weak_alias(__fflush_unlocked, fflush_unlocked);