diff options
author | H. Peter Anvin <hpa@linux.intel.com> | 2012-05-12 15:22:06 -0700 |
---|---|---|
committer | H. Peter Anvin <hpa@linux.intel.com> | 2012-05-12 15:22:06 -0700 |
commit | 370c309d8fc0ad9c919e2689c283ef618d7b5754 (patch) | |
tree | c844d9feba273dc4596a0e5a74c4ba38959f4256 | |
parent | d474cafc40ae5030873ed4d51a62d91146edd0c2 (diff) | |
download | klibc-370c309d8fc0ad9c919e2689c283ef618d7b5754.tar.gz |
[klibc] Fix the zapping of unused input on output operation
It is not supposed to happen, but if we are to discard any buffered
input on output operations, at least do it correctly. This is
functionally equivalent to fseek(..., 0, SEEK_CUR); so implement it
that way.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
-rw-r--r-- | usr/klibc/stdio/fflush.c | 9 | ||||
-rw-r--r-- | usr/klibc/stdio/fwrite.c | 11 |
2 files changed, 10 insertions, 10 deletions
diff --git a/usr/klibc/stdio/fflush.c b/usr/klibc/stdio/fflush.c index 38c708bb33b8a..03fb29618bb87 100644 --- a/usr/klibc/stdio/fflush.c +++ b/usr/klibc/stdio/fflush.c @@ -9,9 +9,12 @@ int __fflush(struct _IO_file_pvt *f) ssize_t rv; char *p; - /* Flush any unused input data */ - f->pub._io_filepos -= f->ibytes; - f->ibytes = 0; + /* + * Flush any unused input data. If there is input data, there + * won't be any output data. + */ + if (__unlikely(f->ibytes)) + return fseek(&f->pub, 0, SEEK_CUR); p = f->buf; while (f->obytes) { diff --git a/usr/klibc/stdio/fwrite.c b/usr/klibc/stdio/fwrite.c index 38e715200fe5d..5931906cce43c 100644 --- a/usr/klibc/stdio/fwrite.c +++ b/usr/klibc/stdio/fwrite.c @@ -14,6 +14,10 @@ static size_t fwrite_noflush(const void *buf, size_t count, ssize_t rv; while (count) { + if (f->ibytes || f->obytes >= f->bufsiz) + if (__fflush(f)) + break; + if (f->obytes == 0 && count >= f->bufsiz) { /* * The buffer is empty and the write is large, @@ -46,13 +50,6 @@ static size_t fwrite_noflush(const void *buf, size_t count, bytes += nb; f->pub._io_filepos += nb; } - - if (!count) - break; /* Done... */ - - /* If we get here, the buffer must be full */ - if (__fflush(f)) - break; } } return bytes; |