getusershell: skip blank lines and comments
[musl.git] / src / stdio / fflush.c
blobb00943764189901ba6f938714561de7466a5dc38
1 #include "stdio_impl.h"
3 /* stdout.c will override this if linked */
4 static FILE *volatile dummy = 0;
5 weak_alias(dummy, __stdout_used);
6 weak_alias(dummy, __stderr_used);
8 int fflush(FILE *f)
10 if (!f) {
11 int r = 0;
12 if (__stdout_used) r |= fflush(__stdout_used);
13 if (__stderr_used) r |= fflush(__stderr_used);
15 for (f=*__ofl_lock(); f; f=f->next) {
16 FLOCK(f);
17 if (f->wpos != f->wbase) r |= fflush(f);
18 FUNLOCK(f);
20 __ofl_unlock();
22 return r;
25 FLOCK(f);
27 /* If writing, flush output */
28 if (f->wpos != f->wbase) {
29 f->write(f, 0, 0);
30 if (!f->wpos) {
31 FUNLOCK(f);
32 return EOF;
36 /* If reading, sync position, per POSIX */
37 if (f->rpos != f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
39 /* Clear read and write modes */
40 f->wpos = f->wbase = f->wend = 0;
41 f->rpos = f->rend = 0;
43 FUNLOCK(f);
44 return 0;
47 weak_alias(fflush, fflush_unlocked);