libc: io: implement file io buffering and internal locking for concurrency

This commit is contained in:
2026-04-01 18:51:13 +01:00
parent f2e650d368
commit 548387c43a
23 changed files with 948 additions and 73 deletions
+12 -16
View File
@@ -3,29 +3,25 @@
#include <errno.h>
#include <stdio.h>
int fgetc(struct __opaque_file *stream)
int __fgetc(struct __opaque_file *stream)
{
if (stream->f_flags & (FILE_EOF | FILE_ERR)) {
return EOF;
}
size_t available = __libc_file_available(stream);
if (available == 0) {
int err = __libc_file_refill(stream);
if (err != SUCCESS) {
__set_errno(err);
stream->f_flags |= FILE_ERR;
return EOF;
}
available = __libc_file_available(stream);
}
if (available == 0) {
stream->f_flags |= FILE_EOF;
char c = 0;
int ret = __libc_file_read(stream, &c, 1);
if (ret < 1) {
return EOF;
}
char c = stream->f_buf[stream->f_buf_readptr++];
return c;
}
int fgetc(struct __opaque_file *stream)
{
__libc_file_lock(stream);
int ret = __fgetc(stream);
__libc_file_unlock(stream);
return ret;
}