commit ce4378b51b24fbceed1bf8308dac1df7ded3aa59
parent a891448dfebfde12ae05c961bb2337d76e72a643
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Mon, 4 Dec 2017 19:12:11 +0100
[lib/c] Add fread() and fwrite()
Diffstat:
3 files changed, 47 insertions(+), 0 deletions(-)
diff --git a/lib/c/src/Makefile b/lib/c/src/Makefile
@@ -12,6 +12,7 @@ OBJ = assert.o strcpy.o strcmp.o strlen.o strchr.o \
localeconv.o atoi.o atol.o atoll.o atexit.o exit.o \
printf.o fprintf.o vfprintf.o \
fgets.o gets.of fgetc.o fputc.o getchar.o putchar.o \
+ fread.o fwrite.o \
realloc.o calloc.o malloc.o
all: $(ARCH)-libc.a
diff --git a/lib/c/src/fread.c b/lib/c/src/fread.c
@@ -0,0 +1,23 @@
+
+#include <stdio.h>
+#undef fread
+
+size_t
+fread(void * restrict ptr, size_t size, size_t nmemb,
+ FILE * restrict fp)
+{
+ unsigned char *bp = ptr;
+ size_t n, i;
+
+ if (nmemb == 0 || size == 0)
+ return 0;
+
+ for (n = 0; n < nmemb; n++) {
+ for (i = 0; i < size; ++i) {
+ if ((*bp++ = getc(fp)) == EOF)
+ return n;
+ }
+ }
+
+ return n;
+}
diff --git a/lib/c/src/fwrite.c b/lib/c/src/fwrite.c
@@ -0,0 +1,23 @@
+
+#include <stdio.h>
+#undef fwrite
+
+size_t
+fwrite(const void * restrict ptr, size_t size, size_t nmemb,
+ FILE * restrict fp)
+{
+ const unsigned char *bp = ptr;
+ size_t n, i;
+
+ if (nmemb == 0 || size == 0)
+ return 0;
+
+ for (n = 0; n < nmemb; n++) {
+ for (i = 0; i < size; ++i) {
+ if (putc(*bp++, fp) == EOF)
+ return n;
+ }
+ }
+
+ return n;
+}