summaryrefslogtreecommitdiff
path: root/src/stdio/fread.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-02-08 18:42:39 -0500
committerJakob Kaivo <jkk@ung.org>2019-02-08 18:42:39 -0500
commit7ef8a7379f7f7d09e71ccae2a0b688c3cd80423f (patch)
tree092ab0aed1769117fd7b28b8592f6f96b0e0d5af /src/stdio/fread.c
parent6acf19370e8adff79cd83b257d3f04aeaf2a59dd (diff)
merge sources into single tree
Diffstat (limited to 'src/stdio/fread.c')
-rw-r--r--src/stdio/fread.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/stdio/fread.c b/src/stdio/fread.c
new file mode 100644
index 00000000..692e3e18
--- /dev/null
+++ b/src/stdio/fread.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include "nonstd/io.h"
+
+/** read directly from a file stream **/
+size_t fread(void * restrict ptr, size_t size, size_t nmemb, FILE * restrict stream)
+{
+ unsigned char *buf = ptr;
+ size_t n = 0;
+ flockfile(stream);
+ while (nmemb) {
+ size_t i;
+ for (i = 0; i < size; i++) {
+ int c = fgetc(stream);
+ if (c == EOF) {
+ /* an error */
+ } else {
+ buf[i] = (char)c;
+ }
+ }
+ nmemb--;
+ buf += size;
+ n++;
+ }
+ funlockfile(stream);
+ /*
+ RETURN_SUCCESS(the number of elements read);
+ */
+ return n;
+}
+
+/***
+reads up to ARGUMENT(nmemb) elements of ARGUMENT(size) bytes each
+from ARGUMENT(stream) into the array at ARGUMENT(ptr).
+
+The file position indicate is advanced by the number of bytes read.
+***/
+/*
+STDC(1)
+*/