ref: ee1f5f77cd79122402b7c823744c933c64e18097
parent: c59e92237fe2e058833fe5744c39ee77325f7d31
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Dec 4 21:16:24 EST 2017
[lib/c] Improve fread() and fwrite()
--- a/lib/c/src/fread.c
+++ b/lib/c/src/fread.c
@@ -9,14 +9,15 @@
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;
--- a/lib/c/src/fwrite.c
+++ b/lib/c/src/fwrite.c
@@ -9,14 +9,16 @@
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;