I/O fixed and tested

This commit is contained in:
Kilian committed 2026-09-28 19:52:08 +02:00
1 parent a0cabac399
commit e6a2af6f8a
4 files changed
+27 -22

No files matched your search

+13 -8
View File
@@ -37,6 +37,10 @@ int closeFile(int fd) {
}
int readByte(const int fd, uint8_t *data) {
if (files[fd] == NULL) {
return STATUS_READY | STATUS_ERROR;
}
int status = 0;
int count = fread(data, 1, 1, files[fd]);
// Error or EOF
@@ -54,17 +58,18 @@ int readByte(const int fd, uint8_t *data) {
}
int writeByte(const int fd, const uint8_t data) {
if (files[fd] == NULL) {
return STATUS_READY | STATUS_ERROR;
}
int status = 0;
int count = fwrite(&data, 1, 1, files[fd]);
// Error or EOF
if (count != 1) {
int error = ferror(files[fd]);
int eof = feof(files[fd]);
if (error != 0) {
status = STATUS_ERROR;
} else if (eof != 0) {
status = STATUS_EOF;
}
if (count != 1 && ferror(files[fd]) != 0) {
return STATUS_READY | STATUS_ERROR;
}
if (fflush(files[fd]) != 0) {
return STATUS_READY | STATUS_ERROR;
}
return STATUS_READY | status;