Make fflush() return 0 on success (#114)

This commit is contained in:
Justine Tunney 2021-03-17 13:02:15 -07:00
parent 4177489762
commit ab64ef7364

View file

@ -32,35 +32,32 @@
* Blocks until data from stream buffer is written out. * Blocks until data from stream buffer is written out.
* *
* @param f is the stream handle * @param f is the stream handle
* @return number of bytes written or -1 on error * @return is 0 on success or -1 on error
*/ */
int fflush(FILE *f) { int fflush(FILE *f) {
size_t i; size_t i;
int res, wrote; int rc, wrote;
res = 0; rc = 0;
if (!f) { if (!f) {
for (i = __fflush.handles.i; i; --i) { for (i = __fflush.handles.i; i; --i) {
if ((f = __fflush.handles.p[i - 1])) { if ((f = __fflush.handles.p[i - 1])) {
if ((wrote = fflush(f)) != -1) { if ((wrote = fflush(f)) == -1) {
res += wrote; rc = -1;
} else {
res = -1;
break; break;
} }
} }
} }
} else if (f->fd != -1) { } else if (f->fd != -1) {
while (f->beg && !f->end) { while (f->beg && !f->end) {
if ((wrote = __fwritebuf(f)) != -1) { if (__fwritebuf(f) == -1) {
res += wrote; rc = -1;
} else {
break; break;
} }
} }
} else if (f->beg && f->beg < f->size) { } else if (f->beg && f->beg < f->size) {
f->buf[f->beg] = 0; f->buf[f->beg] = 0;
} }
return res; return rc;
} }
textstartup int __fflush_register(FILE *f) { textstartup int __fflush_register(FILE *f) {