Fixed flushbuf() so that it works with POSIX conformant Unix versions

This commit is contained in:
ceriel
1991-06-10 17:07:18 +00:00
parent 38562ebeb6
commit 1e2aa9d187
6 changed files with 124 additions and 45 deletions

View File

@@ -14,6 +14,21 @@ int _write(int d, const char *buf, int nbytes);
int _isatty(int d);
extern void (*_clean)(void);
static int
do_write(int d, char *buf, int nbytes)
{
int c;
/* POSIX actually allows write() to return a positive value less
than nbytes, so loop ...
*/
while ((c = _write(d, buf, nbytes)) > 0 && c < nbytes) {
nbytes -= c;
buf += c;
}
return c > 0;
}
int
__flushbuf(int c, FILE * stream)
{
@@ -75,8 +90,8 @@ __flushbuf(int c, FILE * stream)
return EOF;
}
}
if (_write(fileno(stream), (char *)stream->_buf,
-stream->_count) != -stream->_count) {
if (! do_write(fileno(stream), (char *)stream->_buf,
-stream->_count)) {
stream->_flags |= _IOERR;
return EOF;
} else {
@@ -97,8 +112,7 @@ __flushbuf(int c, FILE * stream)
return EOF;
}
}
if (_write(fileno(stream), (char *)stream->_buf, count)
!= count) {
if (! do_write(fileno(stream), (char *)stream->_buf, count)) {
*(stream->_buf) = c;
stream->_flags |= _IOERR;
return EOF;