libc+libos: Properly propagate errors through fgetc() and File::getchar()

This restores proper ^C behavior in the shell.
This commit is contained in:
apio 2023-08-02 14:46:47 +02:00
parent d0ceec6952
commit aac8280e8a
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 11 additions and 4 deletions

View File

@ -432,7 +432,12 @@ extern "C"
{ {
u8 value; u8 value;
ssize_t rc = read_from_buffer(stream, &value, 1); ssize_t rc = read_from_buffer(stream, &value, 1);
if (rc <= 0) return EOF; if (rc < 0)
{
stream->_err = 1;
return EOF;
}
else if (rc == 0) { return EOF; }
return value; return value;
} }

View File

@ -149,9 +149,9 @@ namespace os
int current; int current;
while (true) while (true)
{ {
current = fgetc(m_file); current = TRY(getchar());
if (current == -1) break; if (current == EOF) break;
TRY(data.try_append((char)current)); TRY(data.try_append((char)current));
@ -208,7 +208,9 @@ namespace os
Result<int> File::getchar() Result<int> File::getchar()
{ {
return fgetc(m_file); int rc = fgetc(m_file);
if (rc == EOF && ferror(m_file)) return err(errno);
return rc;
} }
void File::set_close_on_exec() void File::set_close_on_exec()