diff options
author | John Hawthorn <john.hawthorn@gmail.com> | 2016-05-16 19:58:04 -0700 |
---|---|---|
committer | John Hawthorn <john.hawthorn@gmail.com> | 2016-05-16 20:10:12 -0700 |
commit | abe4d5c15876f7e0cb89524a3b2dc0d502f27b38 (patch) | |
tree | dbc6d435304277bf4de8a1c4a9cc7a69782c004f | |
parent | 0bdcf3503e029edfa73f8b8703a8617a1409e1e3 (diff) |
Check errors when opening tty
Opening the tty may fail (for example if the wrong file is specified).
We now print an error and abort when this happens.
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | tty.c | 18 |
2 files changed, 17 insertions, 2 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 8255b7a..4b9fc9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Features: Bugfixes: - Fixed last line of results not being cleared on exit + - Check errors when opening the TTY device ## 0.3 (April 25, 2016) @@ -20,12 +20,26 @@ void tty_close(tty_t *tty) { void tty_init(tty_t *tty, const char *tty_filename) { tty->fdin = open(tty_filename, O_RDONLY); + if (tty->fdin < 0) { + perror("Failed to open tty"); + exit(EXIT_FAILURE); + } + tty->fout = fopen(tty_filename, "w"); - if (setvbuf(tty->fout, NULL, _IOFBF, 4096)) + if (!tty->fout) { + perror("Failed to open tty"); + exit(EXIT_FAILURE); + } + + if (setvbuf(tty->fout, NULL, _IOFBF, 4096)) { perror("setvbuf"); + exit(EXIT_FAILURE); + } - if (tcgetattr(tty->fdin, &tty->original_termios)) + if (tcgetattr(tty->fdin, &tty->original_termios)) { perror("tcgetattr"); + exit(EXIT_FAILURE); + } struct termios new_termios = tty->original_termios; |