Browse code

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.

John Hawthorn authored on 17/05/2016 02:58:04
Showing 2 changed files

... ...
@@ -7,6 +7,7 @@ Features:
7 7
 Bugfixes:
8 8
 
9 9
   - Fixed last line of results not being cleared on exit
10
+  - Check errors when opening the TTY device
10 11
 
11 12
 ## 0.3 (April 25, 2016)
12 13
 
... ...
@@ -20,12 +20,26 @@ void tty_close(tty_t *tty) {
20 20
 
21 21
 void tty_init(tty_t *tty, const char *tty_filename) {
22 22
 	tty->fdin = open(tty_filename, O_RDONLY);
23
+	if (tty->fdin < 0) {
24
+		perror("Failed to open tty");
25
+		exit(EXIT_FAILURE);
26
+	}
27
+
23 28
 	tty->fout = fopen(tty_filename, "w");
24
-	if (setvbuf(tty->fout, NULL, _IOFBF, 4096))
29
+	if (!tty->fout) {
30
+		perror("Failed to open tty");
31
+		exit(EXIT_FAILURE);
32
+	}
33
+
34
+	if (setvbuf(tty->fout, NULL, _IOFBF, 4096)) {
25 35
 		perror("setvbuf");
36
+		exit(EXIT_FAILURE);
37
+	}
26 38
 
27
-	if (tcgetattr(tty->fdin, &tty->original_termios))
39
+	if (tcgetattr(tty->fdin, &tty->original_termios)) {
28 40
 		perror("tcgetattr");
41
+		exit(EXIT_FAILURE);
42
+	}
29 43
 
30 44
 	struct termios new_termios = tty->original_termios;
31 45