forked from Bananymous/banan-os
LibC: fix fread implementation
fread() should read until either size*nitems bytes are read or eof is reached.
This commit is contained in:
parent
afe95be42f
commit
0ec4f970f7
|
@ -255,15 +255,26 @@ size_t fread(void* buffer, size_t size, size_t nitems, FILE* file)
|
||||||
{
|
{
|
||||||
if (file->eof || nitems * size == 0)
|
if (file->eof || nitems * size == 0)
|
||||||
return 0;
|
return 0;
|
||||||
long ret = syscall(SYS_READ, file->fd, buffer, size * nitems);
|
|
||||||
if (ret < 0)
|
size_t target = size * nitems;
|
||||||
|
size_t nread = 0;
|
||||||
|
|
||||||
|
while (nread < target)
|
||||||
{
|
{
|
||||||
|
size_t ret = syscall(SYS_READ, file->fd, (uint8_t*)buffer + nread, target - nread);
|
||||||
|
|
||||||
|
if (ret < 0)
|
||||||
file->error = true;
|
file->error = true;
|
||||||
return 0;
|
else if (ret == 0)
|
||||||
}
|
|
||||||
if (ret == 0)
|
|
||||||
file->eof = true;
|
file->eof = true;
|
||||||
return ret / size;
|
|
||||||
|
if (ret <= 0)
|
||||||
|
return nread;
|
||||||
|
|
||||||
|
nread += ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nread / size;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
|
|
Loading…
Reference in New Issue