shithub: scc

Download patch

ref: c40195789a26f30a3130d44f9ecd8758ea0eb0d4
parent: da7698a0ff0faf4e02663430914b384a2f7276eb
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Dec 4 14:12:11 EST 2017

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

--- a/lib/c/src/Makefile
+++ b/lib/c/src/Makefile
@@ -12,6 +12,7 @@
       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
--- /dev/null
+++ b/lib/c/src/fread.c
@@ -1,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;
+}
--- /dev/null
+++ b/lib/c/src/fwrite.c
@@ -1,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;
+}