diff options
author | Anthony Pesch <inolen@gmail.com> | 2013-08-27 23:43:56 -0700 |
---|---|---|
committer | Anthony Pesch <inolen@gmail.com> | 2013-08-29 12:57:33 -0700 |
commit | fe3533636b24c23fb904f3ca94425003c0565177 (patch) | |
tree | 23ab00295c6a18f00c01690c50de9f53649d6e84 /tests/module/test_stdin.c | |
parent | 4d41dc3fa572f89249749c4b7a37864c99018004 (diff) |
- added tests for tcgetattr / tcsetattr
- made test_stdin async to work in the node environment
- clearerr should reset both eof and error indicators
- fgetc was incorrectly setting the eof indicator. in cases where fread had errored with EAGAIN it was setting eof. I removed the set entirely, as there is no need for fgetc to even worry about it, fread will set the correct value in any case
Diffstat (limited to 'tests/module/test_stdin.c')
-rw-r--r-- | tests/module/test_stdin.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/module/test_stdin.c b/tests/module/test_stdin.c new file mode 100644 index 00000000..ab84fa77 --- /dev/null +++ b/tests/module/test_stdin.c @@ -0,0 +1,48 @@ +#include <errno.h> +#include <fcntl.h> +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#if EMSCRIPTEN +#include <emscripten.h> +#endif + +int line = 0; + +void main_loop(void *arg) +{ + char str[10] = {0}; + int ret; + + if (line == 0) { + ret = fgetc(stdin); + if (ret != EOF) putc(ret, stdout); + if (ret == '\n') line++; + } else if (line > 0) { + ret = scanf("%10s", str); + if (ret > 0) puts(str); + } + + if (ferror(stdin) && errno != EAGAIN) { + puts("error"); + exit(EXIT_FAILURE); + } + + if (feof(stdin)) { + puts("eof"); + exit(EXIT_SUCCESS); + } + + clearerr(stdin); +} + +int main(int argc, char const *argv[]) +{ + fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); +#if EMSCRIPTEN + emscripten_set_main_loop(main_loop, 60, 0); +#else + while (1) main_loop(NULL); sleep(1); +#endif + return 0; +}
\ No newline at end of file |