1
0

fflush.c 883 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "stdio_impl.h"
  2. /* stdout.c will override this if linked */
  3. static FILE *volatile dummy = 0;
  4. weak_alias(dummy, __stdout_used);
  5. weak_alias(dummy, __stderr_used);
  6. int fflush(FILE *f)
  7. {
  8. if (!f) {
  9. int r = 0;
  10. if (__stdout_used) r |= fflush(__stdout_used);
  11. if (__stderr_used) r |= fflush(__stderr_used);
  12. for (f=*__ofl_lock(); f; f=f->next) {
  13. FLOCK(f);
  14. if (f->wpos != f->wbase) r |= fflush(f);
  15. FUNLOCK(f);
  16. }
  17. __ofl_unlock();
  18. return r;
  19. }
  20. FLOCK(f);
  21. /* If writing, flush output */
  22. if (f->wpos != f->wbase) {
  23. f->write(f, 0, 0);
  24. if (!f->wpos) {
  25. FUNLOCK(f);
  26. return EOF;
  27. }
  28. }
  29. /* If reading, sync position, per POSIX */
  30. if (f->rpos != f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
  31. /* Clear read and write modes */
  32. f->wpos = f->wbase = f->wend = 0;
  33. f->rpos = f->rend = 0;
  34. FUNLOCK(f);
  35. return 0;
  36. }
  37. weak_alias(fflush, fflush_unlocked);