scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 1428696a31cfb44b734bb968e69306992c159fb1
parent 12bc42f2e91cedef45f2014cec08ae94264ccdb1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  5 Dec 2017 02:16:24 +0100

[lib/c] Improve fread() and fwrite()

Diffstat:
Mlib/c/src/fread.c | 7++++---
Mlib/c/src/fwrite.c | 12+++++++-----
2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/lib/c/src/fread.c b/lib/c/src/fread.c @@ -9,14 +9,15 @@ fread(void * restrict ptr, size_t size, size_t nmemb, unsigned char *bp = ptr; size_t n, i; - if (nmemb == 0 || size == 0) + if (size == 0) return 0; for (n = 0; n < nmemb; n++) { - for (i = 0; i < size; ++i) { + i = size; + do { if ((*bp++ = getc(fp)) == EOF) return n; - } + } while (--i); } return n; diff --git a/lib/c/src/fwrite.c b/lib/c/src/fwrite.c @@ -9,14 +9,16 @@ fwrite(const void * restrict ptr, size_t size, size_t nmemb, const unsigned char *bp = ptr; size_t n, i; - if (nmemb == 0 || size == 0) + if (size == 0) return 0; for (n = 0; n < nmemb; n++) { - for (i = 0; i < size; ++i) { - if (putc(*bp++, fp) == EOF) - return n; - } + i = size; + do + putc(*bp++, fp); + while (--i); + if (ferror(fp)) + break; } return n;