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)