LibC: Fix fread() EOF behavior with ungetc().

This commit is contained in:
Andreas Kling 2019-03-27 05:13:28 +01:00
parent e145344767
commit f1a2cb0882
Notes: sideshowbarker 2024-07-19 14:55:52 +09:00

View file

@ -146,6 +146,7 @@ int ungetc(int c, FILE* stream)
ASSERT(stream);
stream->have_ungotten = true;
stream->ungotten = c;
stream->eof = false;
return c;
}
@ -209,20 +210,26 @@ size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
if (!size)
return 0;
ssize_t nread = 0;
if (stream->have_ungotten) {
// FIXME: Support ungotten character even if size != 1.
ASSERT(size == 1);
((char*)ptr)[0] = stream->ungotten;
stream->have_ungotten = false;
--nmemb;
if (!nmemb)
return 1;
ptr = &((char*)ptr)[1];
++nread;
}
ssize_t nread = read(stream->fd, ptr, nmemb * size);
if (nread < 0)
ssize_t rc = read(stream->fd, ptr, nmemb * size);
if (rc < 0)
return 0;
if (nread == 0)
if (rc == 0)
stream->eof = true;
nread += rc;
return nread / size;
}