summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2021-01-12 17:02:12 -0700
committermo khan <mo.khan@gmail.com>2021-01-12 17:02:12 -0700
commitd46fdbe3512cc2e09e72a93de5ca23ad21f1cbb7 (patch)
tree66aa0e475ff73793c65e699bd60e19f603103e93
parenta4d1abf7bc2216b50329f9e825b80929ead1fc2f (diff)
add error handling
-rw-r--r--kilo.c15
1 files changed, 11 insertions, 4 deletions
diff --git a/kilo.c b/kilo.c
index 806590c..96822c8 100644
--- a/kilo.c
+++ b/kilo.c
@@ -1,4 +1,5 @@
#include <ctype.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
@@ -6,12 +7,18 @@
struct termios orig_termios;
+void die(const char *s) {
+ perror(s);
+ exit(1);
+}
+
void disable_raw_mode() {
- tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
+ die("tcsetattr");
}
void enable_raw_mode() {
- tcgetattr(STDIN_FILENO, &orig_termios);
+ if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) die("tcgetattr");
atexit(disable_raw_mode);
struct termios raw = orig_termios;
@@ -22,7 +29,7 @@ void enable_raw_mode() {
raw.c_cc[VMIN] = 0;
raw.c_cc[VTIME] = 1;
- tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
}
int main() {
@@ -30,7 +37,7 @@ int main() {
while (1) {
char c = '\0';
- read(STDIN_FILENO, &c, 1);
+ if (read(STDIN_FILENO, &c, 1) == -1 && errno != EAGAIN) die("read");
if (iscntrl(c)) {
printf("%d\r\n", c);
} else {