Skip to content

Commit d3cb972

Browse files
czarnotaPrzemyslaw Czarnota
authored andcommitted
Do not always set stdin to FNONBLOCK when PICO_PLATFORM=host
Because the UART simulation was setting FNONBLOCK flag on stdin, all stdio functions like scanf() or fgets() were non blocking. The workaround is to use a non_blocking_getchar() function to do the UART simulation, which will temporarily set stdin to non blocking. This will make stdio functions blocking like they normally do.
1 parent f396d05 commit d3cb972

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/host/hardware_uart/uart.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,24 @@ void _inittty(void) {
4848
//_tty.c_oflag &= ~ONLCR;
4949
tcsetattr(STDIN_FILENO, TCSANOW, &_tty);
5050

51-
fcntl(STDIN_FILENO, F_SETFL, FNONBLOCK);
5251
atexit(_resettty);
5352
}
5453

54+
static int non_blocking_getchar(void) {
55+
fcntl(STDIN_FILENO, F_SETFL, FNONBLOCK);
56+
57+
int c = getchar();
58+
59+
int old = fcntl(STDIN_FILENO, F_GETFL);
60+
61+
fcntl(STDIN_FILENO, F_SETFL, old & ~FNONBLOCK);
62+
63+
return c;
64+
}
65+
5566
#else
5667
void _inittty() {}
68+
static int non_blocking_getchar(void) { return getchar(); }
5769
#endif
5870

5971
typedef struct {
@@ -67,7 +79,7 @@ static int _nextchar = EOF;
6779

6880
static bool _peekchar() {
6981
if (_nextchar == EOF) {
70-
_nextchar = getchar();
82+
_nextchar = non_blocking_getchar();
7183
}
7284
return _nextchar != EOF;
7385
}

0 commit comments

Comments
 (0)