| 1 | #include "stdio_impl.h" |
| 2 | |
| 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); |
| 7 | |
| 8 | int fflush(FILE *f) |
| 9 | { |
| 10 | 	if (!f) { |
| 11 | 		int r = 0; |
| 12 | 		if (__stdout_used) r |= fflush(__stdout_used); |
| 13 | 		if (__stderr_used) r |= fflush(__stderr_used); |
| 14 | |
| 15 | 		for (f=*__ofl_lock(); f; f=f->next) { |
| 16 | 			FLOCK(f); |
| 17 | 			if (f->wpos != f->wbase) r |= fflush(f); |
| 18 | 			FUNLOCK(f); |
| 19 | 		} |
| 20 | 		__ofl_unlock(); |
| 21 | |
| 22 | 		return r; |
| 23 | 	} |
| 24 | |
| 25 | 	FLOCK(f); |
| 26 | |
| 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; |
| 33 | 		} |
| 34 | 	} |
| 35 | |
| 36 | 	/* If reading, sync position, per POSIX */ |
| 37 | 	if (f->rpos != f->rend) f->seek(f, f->rpos-f->rend, SEEK_CUR); |
| 38 | |
| 39 | 	/* Clear read and write modes */ |
| 40 | 	f->wpos = f->wbase = f->wend = 0; |
| 41 | 	f->rpos = f->rend = 0; |
| 42 | |
| 43 | 	FUNLOCK(f); |
| 44 | 	return 0; |
| 45 | } |
| 46 | |
| 47 | weak_alias(fflush, fflush_unlocked); |