[1] | 1 | #include <unistd.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include "dietwarning.h" |
---|
| 5 | #include "dietstdio.h" |
---|
| 6 | |
---|
| 7 | FILE *__stdio_root = NULL; |
---|
| 8 | |
---|
| 9 | int __stdio_atexit=0; |
---|
| 10 | |
---|
| 11 | int fflush(FILE *stream) __attribute__((weak,alias("fflush_unlocked"))); |
---|
| 12 | |
---|
| 13 | void __stdio_flushall(void) { |
---|
| 14 | fflush(0); |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | int fflush_unlocked(FILE *stream) { |
---|
| 18 | if (stream==0) { |
---|
| 19 | int res; |
---|
| 20 | FILE *f; |
---|
| 21 | __fflush_stdin(); |
---|
| 22 | __fflush_stdout(); |
---|
| 23 | __fflush_stderr(); |
---|
| 24 | for (res=0, f=__stdio_root; f; f=f->next) |
---|
| 25 | if (fflush(f)) |
---|
| 26 | res=-1; |
---|
| 27 | return res; |
---|
| 28 | } |
---|
| 29 | // if (stream->flags&NOBUF) return 0; |
---|
| 30 | |
---|
| 31 | if (stream->flags&BUFINPUT) { |
---|
| 32 | register int tmp; |
---|
| 33 | if ((tmp=stream->bm-stream->bs)) { |
---|
| 34 | lseek(stream->fd,tmp,SEEK_CUR); |
---|
| 35 | } |
---|
| 36 | stream->bs=stream->bm=0; |
---|
| 37 | } else { |
---|
| 38 | if (stream->bm && write(stream->fd,stream->buf,stream->bm)!=(ssize_t)stream->bm) { |
---|
| 39 | stream->flags|=ERRORINDICATOR; |
---|
| 40 | return -1; |
---|
| 41 | } |
---|
| 42 | stream->bm=0; |
---|
| 43 | } |
---|
| 44 | return 0; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | int __fflush4(FILE *stream,int next) { |
---|
| 48 | if (!__stdio_atexit) { |
---|
| 49 | __stdio_atexit=1; |
---|
| 50 | atexit(__stdio_flushall); |
---|
| 51 | } |
---|
| 52 | if ((stream->flags & BUFINPUT) != next) { |
---|
| 53 | int res=fflush_unlocked(stream); |
---|
| 54 | stream->flags=(stream->flags&~BUFINPUT)|next; |
---|
| 55 | return res; |
---|
| 56 | } |
---|
| 57 | if (stream->fd==0 && __stdin_is_tty()) __fflush_stdout(); |
---|
| 58 | return 0; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /* Internal function, has no prototype. |
---|
| 62 | * This is defined here because of the weak symbol ELF semantics */ |
---|
| 63 | int __stdio_outs(const char *s,size_t len); |
---|
| 64 | int __stdio_outs(const char *s,size_t len) { |
---|
| 65 | return fwrite(s,1,(size_t)len,stdout)==len?1:0; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | link_warning("fflush","warning: your code uses stdio (7+k bloat).") |
---|