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