avoid referencing address of extern function from vdprintf
[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->buf && __oflow(f)) return -1;
7
8         /* If reading, sync position, per POSIX */
9         if (f->rpos < f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR);
10         f->rpos = f->rend;
11
12         /* Hook for special behavior on flush */
13         if (f->flush) f->flush(f);
14
15         return (f->flags & F_ERR) ? EOF : 0;
16 }
17
18 /* stdout.c will override this if linked */
19 static FILE *const __dummy = 0;
20 weak_alias(__dummy, __stdout_to_flush);
21
22 int fflush(FILE *f)
23 {
24         int r;
25         FILE *next;
26
27         if (f) {
28                 FLOCK(f);
29                 r = __fflush_unlocked(f);
30                 FUNLOCK(f);
31                 return r;
32         }
33
34         r = __stdout_to_flush ? fflush(__stdout_to_flush) : 0;
35
36         OFLLOCK();
37         for (f=ofl_head; f; f=next) {
38                 FLOCK(f);
39                 OFLUNLOCK();
40                 r |= __fflush_unlocked(f);
41                 OFLLOCK();
42                 next = f->next;
43                 FUNLOCK(f);
44         }
45         OFLUNLOCK();
46         
47         return r;
48 }
49
50 weak_alias(__fflush_unlocked, fflush_unlocked);