Skip to contentSkip to conversation

nolibc: Add fread() and fseek()

PATCH Superseded Daniel Palmer 4 participants 15 messages Jan 4 – Jan 6

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover nolibc: Add fread() and fseek()

Using nolibc on nommu 68000, something I'm trying to build[0] wanted fread() and fseek() so I added them, they seem to work so I'm sharing them. Not sure if I need to add tests or something? Maybe there is a reason these weren't implemented in the first place...

0 - https://github.com/FrenkelS/Doom8088ST/issues/21

Daniel Palmer (2):
tools/nolibc: Add fread() to stdio.h
tools/nolibc: Add fseek() to stdio.h

 tools/include/nolibc/stdio.h | 54 +++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/2 tools/nolibc: Add fread() to stdio.h

Add a very basic version of fread() like we already have for fwrite().

---
 tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
tools/include/nolibc/stdio.h +34 -1
index 1f16dab2ac88..21569ebae824 100644
@@ -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,39 @@ 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)
+{
+	ssize_t ret;
+	int fd = fileno(stream);
+
+	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 readed;
+
+	for (readed = 0; readed < nmemb; readed++) {
+		if (_fread(s, size, stream) != 0)
+			break;
+		s += size;
+	}
+	return readed;
+}
+
 static __attribute__((unused))
 int fputs(const char *s, FILE *stream)
 {
Signature · 1 line
2.51.0
#2 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Quoted 22 lines · Daniel Palmer

Add a very basic version of fread() like we already have for fwrite().

---
 tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
tools/include/nolibc/stdio.h +1 -1
index 1f16dab2ac88..21569ebae824 100644
@@ -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,39 @@ size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
 	return written;

tools/include/nolibc/stdio.h size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) · line 214

Quoted 10 lines
tools/include/nolibc/stdio.h +8 -0
 }
 
+/* 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)
+{
+	ssize_t ret;
+	int fd = fileno(stream);

Switch these two lines around.

Quoted 6 lines
tools/include/nolibc/stdio.h +6 -0
+
+	while (size) {
+		ret = read(fd, buf, size);
+		if (ret <= 0)
+			return EOF;
+		size -= ret;

tools/include/nolibc/stdio.h size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) · line 230

Quoted 10 lines
tools/include/nolibc/stdio.h +10 -0
+		buf += ret;
+	}
+	return 0;
+}
+
+
+static __attribute__((unused))
+size_t fread(void *s, size_t size, size_t nmemb, FILE *stream)
+{
+	size_t readed;

I really don't like this name. Maybe 'nread'?

Quoted 15 lines
tools/include/nolibc/stdio.h +9 -0
+
+	for (readed = 0; readed < nmemb; readed++) {
+		if (_fread(s, size, stream) != 0)
+			break;
+		s += size;
+	}
+	return readed;
+}
+
 static __attribute__((unused))
 int fputs(const char *s, FILE *stream)
 {
Signature · 1 line
2.51.0
#3 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

In reply to Daniel Palmer
Quoted 27 lines · On Sun, 4 Jan 2026 17:38:36 +0900 Daniel Palmer <[email protected]> wrote:

Add a very basic version of fread() like we already have for fwrite().

---
 tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
tools/include/nolibc/stdio.h +4 -1
index 1f16dab2ac88..21569ebae824 100644
@@ -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,39 @@ 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.
+ */

tools/include/nolibc/stdio.h size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) · line 219

Quoted 10 lines
tools/include/nolibc/stdio.h +10 -0
+static __attribute__((unused))
+int _fread(void *buf, size_t size, FILE *stream)
+{
+	ssize_t ret;
+	int fd = fileno(stream);
+
+	while (size) {
+		ret = read(fd, buf, size);
+		if (ret <= 0)
+			return EOF;

You need to return a partial length if some data was read before EOF.

tools/include/nolibc/stdio.h size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) · line 230

Quoted 11 lines
tools/include/nolibc/stdio.h +11 -0
+		size -= ret;
+		buf += ret;
+	}
+	return 0;
+}
+
+
+static __attribute__((unused))
+size_t fread(void *s, size_t size, size_t nmemb, FILE *stream)
+{
+	size_t readed;

Isn't it enough to just multiply 'size' and 'nmemb' together and then do a single long read() (maybe with retries for short reads). The multiply can't overflow (for a valid request) because the buffer size is also limited to 'size_t'.

OTOH Linux limits the maximum return from read() to INT_MAX - PAGE_SIZE (even on 64bit).

David

Quoted 12 lines
tools/include/nolibc/stdio.h +9 -0
+
+	for (readed = 0; readed < nmemb; readed++) {
+		if (_fread(s, size, stream) != 0)
+			break;
+		s += size;
+	}
+	return readed;
+}
+
 static __attribute__((unused))
 int fputs(const char *s, FILE *stream)
 {
#4 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Hi David,

Quoted 4 lines · David Laight
Quoted 4 lines
tools/include/nolibc/stdio.h +4 -0
+static __attribute__((unused))
+int _fread(void *buf, size_t size, FILE *stream)
+{
+     ssize_t ret;
Quoted 8 lines

tools/include/nolibc/stdio.h size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) · line 219

Quoted 6 lines
tools/include/nolibc/stdio.h +6 -0
+     int fd = fileno(stream);
+
+     while (size) {
+             ret = read(fd, buf, size);
+             if (ret <= 0)
+                     return EOF;

You need to return a partial length if some data was read before EOF.

According to the man page:

On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

So I think the current logic is correct? If you have a file that has a length that is not a multiple of the item size and try to read more items than possible (i.e. you have 3 bytes in the file, you try to read 2 2 byte items) we read and count the items that are possible, the partial data is read but the read loop returns EOF, the partial item isn't counted and fread() returns the number of fully read items. _fread() could return the amount that was partially read but fread() is only checking for non-zero so there wouldn't be any difference.

Maybe I'm missing something?

Cheers,

Daniel

#5 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Quoted 23 lines · On Mon, 5 Jan 2026 09:54:10 +0900 Daniel Palmer <[email protected]> wrote:

Hi David,

Quoted 4 lines · David Laight
Quoted 4 lines
tools/include/nolibc/stdio.h +4 -0
+static __attribute__((unused))
+int _fread(void *buf, size_t size, FILE *stream)
+{
+     ssize_t ret;
Quoted 8 lines

tools/include/nolibc/stdio.h size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) · line 219

Quoted 6 lines
tools/include/nolibc/stdio.h +6 -0
+     int fd = fileno(stream);
+
+     while (size) {
+             ret = read(fd, buf, size);
+             if (ret <= 0)
+                     return EOF;  

You need to return a partial length if some data was read before EOF.

According to the man page:

On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).

Quoted 6 lines

So I think the current logic is correct? If you have a file that has a length that is not a multiple of the item size and try to read more items than possible (i.e. you have 3 bytes in the file, you try to read 2 2 byte items) we read and count the items that are possible, the partial data is read but the read loop returns EOF, the partial item isn't counted and fread() returns the number of fully read items.

But you've deleted the partial bytes from the input.
I'm sure that isn't right.
Normally a FILE is buffered and the bytes are saved for the next read. Remember you can be reading from a pipe that is being written using 'block buffering' - so it is valid for only a partial 'item' be read. (I'm sure non-blocking IO is also valid...)

Quoted 4 lines

_fread() could return the amount that was partially read but fread() is only checking for non-zero so there wouldn't be any difference.

Maybe I'm missing something?

What you are doing is equivalent to a long read and then dividing the number of bytes read by the item size.
So you might as well do a single read system call.

David

Quoted 4 lines

Cheers,

Daniel

#6 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Hi David,

Quoted 6 lines · David Laight

But you've deleted the partial bytes from the input.
I'm sure that isn't right.
Normally a FILE is buffered and the bytes are saved for the next read. Remember you can be reading from a pipe that is being written using 'block buffering' - so it is valid for only a partial 'item' be read. (I'm sure non-blocking IO is also valid...)

I see now. If a partial read happens, the next call to fread() will read from after the end of the partial read that happened and it'll be broken.
Since in nolibc the FILE pointer that gets used isn't really a pointer but the file descriptor I'm not sure where we'd stash the partial part so we need to avoid doing the partial read entirely.

Thanks,

Daniel

#7 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Quoted 10 lines · On Mon, 5 Jan 2026 18:43:03 +0900 Daniel Palmer <[email protected]> wrote:

Hi David,

Quoted 6 lines · David Laight

But you've deleted the partial bytes from the input.
I'm sure that isn't right.
Normally a FILE is buffered and the bytes are saved for the next read. Remember you can be reading from a pipe that is being written using 'block buffering' - so it is valid for only a partial 'item' be read. (I'm sure non-blocking IO is also valid...)

Quoted 6 lines

I see now. If a partial read happens, the next call to fread() will read from after the end of the partial read that happened and it'll be broken.
Since in nolibc the FILE pointer that gets used isn't really a pointer but the file descriptor I'm not sure where we'd stash the partial part so we need to avoid doing the partial read entirely.

Except you can't really avoid the partial read.
Doing multiple read() system calls doesn't help.
The situation where it can happen probably doesn't happen for nolibc. Is there support for ferror() and/or feof() ?
(a global 'u8 fstate[64]' indexed by fd number would probably suffice.) If so you could set the 'error' bit and then error any further fread()s.

David

Quoted 4 lines

Thanks,

Daniel

#8 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Quoted 17 lines · David Laight
Quoted 8 lines · On Mon, 5 Jan 2026 18:43:03 +0900 Daniel Palmer <[email protected]> wrote:
Quoted 6 lines · David Laight

But you've deleted the partial bytes from the input.
I'm sure that isn't right.
Normally a FILE is buffered and the bytes are saved for the next read. Remember you can be reading from a pipe that is being written using 'block buffering' - so it is valid for only a partial 'item' be read. (I'm sure non-blocking IO is also valid...)

Quoted 6 lines

I see now. If a partial read happens, the next call to fread() will read from after the end of the partial read that happened and it'll be broken.
Since in nolibc the FILE pointer that gets used isn't really a pointer but the file descriptor I'm not sure where we'd stash the partial part so we need to avoid doing the partial read entirely.

Quoted 7 lines

Except you can't really avoid the partial read.
Doing multiple read() system calls doesn't help.
The situation where it can happen probably doesn't happen for nolibc.

Is there support for ferror() and/or feof() ?
(a global 'u8 fstate[64]' indexed by fd number would probably suffice.) If so you could set the 'error' bit and then error any further fread()s.

No, neither of them is currently supported.
Given that the support for 'FILE *' is obviously somewhat limited in general and the existing fwrite() shares the same issues, I am leaning towards picking up these patches. We can then find a proper solution.

Users trying to use these APIs really correctly will quite fast find out that ferror() and feof() are missing.

The only real solution for partial writes that I see with the current architecture is to limit the 'size' argument to '1'.
Maybe even with a static assertion. But there still won't be any way to properly signal the issue to the caller. Not that ferror() could distinguish the types of error in any case...

Thomas

maintainer
#9 lore raw

Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

Quoted 26 lines · Thomas Weißschuh
Quoted 17 lines · David Laight
Quoted 8 lines · On Mon, 5 Jan 2026 18:43:03 +0900 Daniel Palmer <[email protected]> wrote:
Quoted 6 lines · David Laight

But you've deleted the partial bytes from the input.
I'm sure that isn't right.
Normally a FILE is buffered and the bytes are saved for the next read. Remember you can be reading from a pipe that is being written using 'block buffering' - so it is valid for only a partial 'item' be read. (I'm sure non-blocking IO is also valid...)

Quoted 6 lines

I see now. If a partial read happens, the next call to fread() will read from after the end of the partial read that happened and it'll be broken.
Since in nolibc the FILE pointer that gets used isn't really a pointer but the file descriptor I'm not sure where we'd stash the partial part so we need to avoid doing the partial read entirely.

Quoted 7 lines

Except you can't really avoid the partial read.
Doing multiple read() system calls doesn't help.
The situation where it can happen probably doesn't happen for nolibc.

Is there support for ferror() and/or feof() ?
(a global 'u8 fstate[64]' indexed by fd number would probably suffice.) If so you could set the 'error' bit and then error any further fread()s.

Quoted 7 lines

No, neither of them is currently supported.
Given that the support for 'FILE *' is obviously somewhat limited in general and the existing fwrite() shares the same issues, I am leaning towards picking up these patches. We can then find a proper solution.

Users trying to use these APIs really correctly will quite fast find out that ferror() and feof() are missing.

I agree with you. The lib's called "nolibc" for a reason: it's not a full-featured libc. Users have to accept some limitations.

Quoted 2 lines

The only real solution for partial writes that I see with the current architecture is to limit the 'size' argument to '1'.

I thought about this as well but I don't know if it will be more annoying than helpful...

Quoted 3 lines

Maybe even with a static assertion. But there still won't be any way to properly signal the issue to the caller. Not that ferror() could distinguish the types of error in any case...

Let's see with the current patch how it goes. The first reported issues (if any) will help us lean in the most desirable direction.

Willy

#10 lore raw

PATCH 2/2 tools/nolibc: Add fseek() to stdio.h

In reply to Daniel Palmer

A very basic wrapper around lseek() that implements fseek().

---
 tools/include/nolibc/stdio.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
tools/include/nolibc/stdio.h +19 -0
index 21569ebae824..d352ffa655a7 100644
@@ -273,6 +273,25 @@ char *fgets(char *s, int size, FILE *stream)
 }
 
 
+/* fseek */
+static __attribute__((unused))
+int fseek(FILE *stream, long offset, int whence)
+{
+	int fd = fileno(stream);
+	off_t ret;
+
+	ret = lseek(fd, offset, whence);
+
+	/* lseek() and fseek() differ in that lseek returns the new
+	 * position or -1, fseek() returns either 0 or -1.
+	 */
+	if (ret >= 0)
+		return 0;
+
+	return -1;
+}
+
+
 /* minimal printf(). It supports the following formats:
  *  - %[l*]{d,u,c,x,p}
  *  - %s
Signature · 1 line
2.51.0
#11 lore raw
In reply to Daniel Palmer
Quoted 3 lines · Daniel Palmer

Using nolibc on nommu 68000, something I'm trying to build[0] wanted fread() and fseek() so I added them, they seem to work so I'm sharing them. Not sure if I need to add tests or something? Maybe there is a reason these weren't implemented in the first place...

Thanks! These look good in general, I'll reply inline with some comments. Please also do add some tests.

Quoted 12 lines

0 - https://github.com/FrenkelS/Doom8088ST/issues/21

Daniel Palmer (2):
tools/nolibc: Add fread() to stdio.h
tools/nolibc: Add fseek() to stdio.h

 tools/include/nolibc/stdio.h | 54 +++++++++++++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 1 deletion(-)
Signature · 1 line
2.51.0
#12 lore raw

Hi Thomas,

Quoted 1 line · Thomas Weißschuh

Please also do add some tests.

Would a single function test that exercises the new functions be enough? Something like this:

tools/testing/selftests/nolibc/nolibc-test.c +0 -0
@@ -877,6 +877,45 @@ int test_file_stream(void)
       return 0;

}

+int test_file_stream_wsr(void)
+{

+       const char dataout[] = "foo";
+       const size_t datasz = sizeof(dataout);
+       char datain[datasz];
+       FILE *f;
+       int r;

+
+ f = fopen("/tmp/file_stream_test", "w+");
+ if (!f)
+ return -1;
+
+ r = fwrite(dataout, 1, datasz, f);
+ if (r != datasz)
+ goto fail;
+
+ r = fseek(f, 0, SEEK_SET);
+ if (r)
+ goto fail;
+
+ r = fread(datain, 1, datasz, f);
+ if (r != datasz)
+ goto fail;
+
+ if (memcmp(datain, dataout, datasz) != 0)
+ goto fail;
+
+ r = fclose(f);
+ if (r == EOF)
+ return -1;
+
+ return 0;
+
+fail:
+ fclose(f);
+ return -1;
+}
+

Cheers,

Daniel

maintainer
#13 lore raw

Hi Daniel,

Quoted 42 lines · Daniel Palmer

Hi Thomas,

Quoted 1 line · Thomas Weißschuh

Please also do add some tests.

Would a single function test that exercises the new functions be enough? Something like this:

tools/testing/selftests/nolibc/nolibc-test.c +28 -0
@@ -877,6 +877,45 @@ int test_file_stream(void)
       return 0;
 }
 
+int test_file_stream_wsr(void)
+{
+       const char dataout[] = "foo";
+       const size_t datasz = sizeof(dataout);
+       char datain[datasz];
+       FILE *f;
+       int r;
+
+       f = fopen("/tmp/file_stream_test", "w+");
+       if (!f)
+               return -1;
+
+       r = fwrite(dataout, 1, datasz, f);
+       if (r != datasz)
+               goto fail;
+
+       r = fseek(f, 0, SEEK_SET);
+       if (r)
+               goto fail;
+
+       r = fread(datain, 1, datasz, f);
+       if (r != datasz)
+               goto fail;
+
+       if (memcmp(datain, dataout, datasz) != 0)
+               goto fail;
+
+       r = fclose(f);

tools/testing/selftests/nolibc/nolibc-test.c int test_file_stream(void) · line 917

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+       if (r == EOF)
+               return -1;
+
+       return 0;
+
+fail:
+       fclose(f);
+       return -1;
+}
+

In general it's preferable when tests focus on a specific syscall or function, so that when a failure is reported, it's easier to figure what we broke. But I agree it's not always easy, especially when resources are allocated and released, like here. I can hardly propose better. Please also keep in mind that sometimes we also want to validate certain error cases (e.g. if opening a file in read-only mode, writing to it should fail).

Hoping this helps,
Willy

#14 lore raw
In reply to Daniel Palmer
Quoted 7 lines · Daniel Palmer

Hi Thomas,

Quoted 1 line · Thomas Weißschuh

Please also do add some tests.

Would a single function test that exercises the new functions be enough? Something like this:

That example looks good. But please use O_TMPFILE for the test file, to make the test more robust and also test fdopen().
You can also return different error numbers depending on the failure to make debugging easier.

Quoted 48 lines
tools/testing/selftests/nolibc/nolibc-test.c +38 -0
@@ -877,6 +877,45 @@ int test_file_stream(void)
       return 0;
 }
 
+int test_file_stream_wsr(void)
+{
+       const char dataout[] = "foo";
+       const size_t datasz = sizeof(dataout);
+       char datain[datasz];
+       FILE *f;
+       int r;
+
+       f = fopen("/tmp/file_stream_test", "w+");
+       if (!f)
+               return -1;
+
+       r = fwrite(dataout, 1, datasz, f);
+       if (r != datasz)
+               goto fail;
+
+       r = fseek(f, 0, SEEK_SET);
+       if (r)
+               goto fail;
+
+       r = fread(datain, 1, datasz, f);
+       if (r != datasz)
+               goto fail;
+
+       if (memcmp(datain, dataout, datasz) != 0)
+               goto fail;
+
+       r = fclose(f);
+       if (r == EOF)
+               return -1;
+
+       return 0;
+
+fail:
+       fclose(f);
+       return -1;
+}
+
 
 Cheers,
 

Daniel