summaryrefslogtreecommitdiff
path: root/tools/include
diff options
context:
space:
mode:
authorDaniel Palmer <daniel@thingy.jp>2026-01-05 11:36:27 +0900
committerThomas Weißschuh <linux@weissschuh.net>2026-01-11 12:47:47 +0100
commitedaf30743185f6ed8e29dcb2f1d01e183c0b807b (patch)
tree516146f1f4ef18daf4567fe8993b4bbbe5dcc0bc /tools/include
parent6fe8360b16acbfb50c703f52568cad46759be2ed (diff)
tools/nolibc: Add fread() to stdio.h
Add a very basic version of fread() like we already have for fwrite(). Signed-off-by: Daniel Palmer <daniel@thingy.jp> Link: https://patch.msgid.link/20260105023629.1502801-2-daniel@thingy.jp Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Diffstat (limited to 'tools/include')
-rw-r--r--tools/include/nolibc/stdio.h34
1 files changed, 33 insertions, 1 deletions
diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
index 1f16dab2ac88..6904252df97d 100644
--- a/tools/include/nolibc/stdio.h
+++ b/tools/include/nolibc/stdio.h
@@ -170,7 +170,7 @@ int putchar(int c)
}
-/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
+/* fwrite(), fread(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
/* internal fwrite()-like function which only takes a size and returns 0 on
* success or EOF on error. It automatically retries on short writes.
@@ -204,6 +204,38 @@ size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
return written;
}
+/* internal fread()-like function which only takes a size and returns 0 on
+ * success or EOF on error. It automatically retries on short reads.
+ */
+static __attribute__((unused))
+int _fread(void *buf, size_t size, FILE *stream)
+{
+ int fd = fileno(stream);
+ ssize_t ret;
+
+ while (size) {
+ ret = read(fd, buf, size);
+ if (ret <= 0)
+ return EOF;
+ size -= ret;
+ buf += ret;
+ }
+ return 0;
+}
+
+static __attribute__((unused))
+size_t fread(void *s, size_t size, size_t nmemb, FILE *stream)
+{
+ size_t nread;
+
+ for (nread = 0; nread < nmemb; nread++) {
+ if (_fread(s, size, stream) != 0)
+ break;
+ s += size;
+ }
+ return nread;
+}
+
static __attribute__((unused))
int fputs(const char *s, FILE *stream)
{