mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibC: Fix fread() EOF behavior with ungetc().
This commit is contained in:
parent
e145344767
commit
f1a2cb0882
Notes:
sideshowbarker
2024-07-19 14:55:52 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f1a2cb0882b
1 changed files with 10 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue