1
0

fflush.c 876 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. return 0;
  15. }
  16. /* stdout.c will override this if linked */
  17. static FILE *volatile dummy = 0;
  18. weak_alias(dummy, __stdout_used);
  19. int fflush(FILE *f)
  20. {
  21. int r;
  22. if (f) {
  23. FLOCK(f);
  24. r = __fflush_unlocked(f);
  25. FUNLOCK(f);
  26. return r;
  27. }
  28. r = __stdout_used ? fflush(__stdout_used) : 0;
  29. OFLLOCK();
  30. for (f=libc.ofl_head; f; f=f->next) {
  31. FLOCK(f);
  32. if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
  33. FUNLOCK(f);
  34. }
  35. OFLUNLOCK();
  36. return r;
  37. }
  38. weak_alias(__fflush_unlocked, fflush_unlocked);