commit 6d2fbe2b539e6d507fd4786ec01a82c527c9d4d0 Author: lw-everestlinux Date: Fri Dec 16 16:47:38 2022 -0500 init diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..92d9ca0 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +k2: src/k2.c + $(CC) src/k2.c -o k2 -Wall -Wextra -pedantic -std=c99 diff --git a/src/k2.c b/src/k2.c new file mode 100644 index 0000000..6444a10 --- /dev/null +++ b/src/k2.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include + +struct termios orig_termios; + +void DISABLE_RAW_MODE() { + tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios); +} + +void ENABLE_RAW_MODE() { + tcgetattr(STDIN_FILENO, &orig_termios); + atexit(DISABLE_RAW_MODE); + struct termios raw = orig_termios; + raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); + raw.c_oflag &= ~(OPOST); + raw.c_cflag != (CS8); + raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); + raw.c_cc[VMIN] = 0; + raw.c_cc[VTIME] = 1; + tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw); +} + +int main() { + ENABLE_RAW_MODE(); + char c; + while (read(STDIN_FILENO, &c, 1) == 1 && c != 'q') { + if (iscntrl(c)) { + printf("%d\r\n", c); + } else { + printf("%d ('%c')\r\n", c, c); + } + } + return 0; +}