Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions sources/fread.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,36 @@

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t bytes = size * nmemb;
long rc = Fread(FILE_GET_HANDLE(stream), bytes, ptr);
size_t bytes;
size_t got = 0;
long rc;

if (size == 0 || nmemb == 0)
return 0;

bytes = size * nmemb;

if (stream->__pushback != EOF)
{
/* a character pushed back by ungetc() comes first */
*(unsigned char *)ptr = (unsigned char)stream->__pushback;
stream->__pushback = EOF;
ptr = (unsigned char *)ptr + 1;
--bytes;
++got;
}

rc = bytes > 0 ? Fread(FILE_GET_HANDLE(stream), bytes, ptr) : 0;

if (rc < 0)
{
stream->__error = 1;
__set_errno(-rc);
rc = 0;
} else
{
if (rc < bytes)
if ((size_t)rc < bytes)
stream->__eof = 1;
rc /= size;
got += rc;
}
return rc;
return got / size;
}
10 changes: 8 additions & 2 deletions sources/fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
long rc;
size_t n = size * nmemb;

size_t n;
#ifdef STDIO_MAP_NEWLINE
int fd = (int)FILE_GET_HANDLE(stream);
#endif

if (size == 0 || nmemb == 0)
return 0;

n = size * nmemb;

#ifdef STDIO_MAP_NEWLINE
if (stream->__mode.__binary)
{
rc = Fwrite(fd, n, ptr);
Expand Down
Loading