Fix reading stdin after EOF. (#3435)
This commit is contained in:
@@ -78,18 +78,12 @@ int32_t consoleReadUtf8(void* utf8, uint32_t maxSizeBytes) {
|
|||||||
#ifdef KONAN_ZEPHYR
|
#ifdef KONAN_ZEPHYR
|
||||||
return 0;
|
return 0;
|
||||||
#else
|
#else
|
||||||
#ifdef KONAN_WASM
|
auto length = ::read(STDIN_FILENO, utf8, maxSizeBytes - 1);
|
||||||
FILE* file = nullptr;
|
if (length <= 0) return -1;
|
||||||
#else
|
char* start = reinterpret_cast<char*>(utf8);
|
||||||
FILE* file = stdin;
|
char* current = start + length - 1;
|
||||||
#endif
|
|
||||||
char* result = ::fgets(reinterpret_cast<char*>(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;
|
|
||||||
bool isTrimming = true;
|
bool isTrimming = true;
|
||||||
while (current >= result && isTrimming) {
|
while (current >= start && isTrimming) {
|
||||||
switch (*current) {
|
switch (*current) {
|
||||||
case '\n':
|
case '\n':
|
||||||
case '\r':
|
case '\r':
|
||||||
|
|||||||
Reference in New Issue
Block a user