From b1710e875874e8bc42bd6ab7e1e38d3447ff7118 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Tue, 8 Oct 2019 11:05:33 +0300 Subject: [PATCH] Fix reading stdin after EOF. (#3435) --- runtime/src/main/cpp/Porting.cpp | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/runtime/src/main/cpp/Porting.cpp b/runtime/src/main/cpp/Porting.cpp index b0a5e0b0520..21428161691 100644 --- a/runtime/src/main/cpp/Porting.cpp +++ b/runtime/src/main/cpp/Porting.cpp @@ -78,18 +78,12 @@ int32_t consoleReadUtf8(void* utf8, uint32_t maxSizeBytes) { #ifdef KONAN_ZEPHYR return 0; #else -#ifdef KONAN_WASM - FILE* file = nullptr; -#else - FILE* file = stdin; -#endif - char* result = ::fgets(reinterpret_cast(utf8), maxSizeBytes - 1, file); - if (result == nullptr) return -1; - int32_t length = ::strlen(result); - // fgets reads until EOF or newline so we need to remove linefeeds. - char* current = result + length - 1; + auto length = ::read(STDIN_FILENO, utf8, maxSizeBytes - 1); + if (length <= 0) return -1; + char* start = reinterpret_cast(utf8); + char* current = start + length - 1; bool isTrimming = true; - while (current >= result && isTrimming) { + while (current >= start && isTrimming) { switch (*current) { case '\n': case '\r':