Skip to content

Instantly share code, notes, and snippets.

@dagon666
Created December 31, 2013 10:08
Show Gist options
  • Save dagon666/8194870 to your computer and use it in GitHub Desktop.
Save dagon666/8194870 to your computer and use it in GitHub Desktop.
linux tty with canonical mode turned off - non blocking getchar()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
void cm_off(void)
{
struct termios t;
tcgetattr(STDIN_FILENO, &t);
t.c_lflag &= ~ICANON;
t.c_lflag &= ~ECHO;
// Apply the new settings
tcsetattr(STDIN_FILENO, TCSANOW, &t);
}
void cm_on(void)
{
struct termios t;
tcgetattr(STDIN_FILENO, &t);
t.c_lflag |= ICANON;
t.c_lflag |= ECHO;
// Apply the new settings
tcsetattr(STDIN_FILENO, TCSANOW, &t);
}
int main(int argc, char const *argv[])
{
cm_off();
while (1) {
printf("%02x\n", (unsigned char)getchar());
usleep(300000);
}
cm_on();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment