From 77022abafdc60fc965de0b9667f7272e9aaffb0a Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 22 Jul 2023 11:25:20 +0200 Subject: [PATCH] libc: Implement ungetc --- libc/src/stdio.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libc/src/stdio.cpp b/libc/src/stdio.cpp index 4e3440ff..6e84e267 100644 --- a/libc/src/stdio.cpp +++ b/libc/src/stdio.cpp @@ -688,9 +688,18 @@ extern "C" return f; } - int ungetc(int, FILE*) + int ungetc(int c, FILE* stream) { - fail("FIXME: ungetc: not implemented"); + if (stream->_buf.index == 0) + return EOF; // No data currently in the read buffer, or no data has been read from it. + + if (stream->_buf.mode == _IONBF) + return EOF; // FIXME: C doesn't state that ungetc() should only work on buffered streams. + + stream->_buf.index--; + stream->_buf.buffer[stream->_buf.index] = (char)c; + + return 0; } int setvbuf(FILE* stream, char* buf, int mode, size_t size)