This commit is contained in:
lw-everestlinux 2022-12-16 16:47:38 -05:00
commit 6d2fbe2b53
2 changed files with 39 additions and 0 deletions

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
k2: src/k2.c
$(CC) src/k2.c -o k2 -Wall -Wextra -pedantic -std=c99

37
src/k2.c Normal file
View File

@ -0,0 +1,37 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
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;
}