Skip to content

Instantly share code, notes, and snippets.

@RodrigoDornelles
Created December 20, 2022 20:47
Show Gist options
  • Save RodrigoDornelles/698dba271f1f25c78a96d24a8a649b4d to your computer and use it in GitHub Desktop.
Save RodrigoDornelles/698dba271f1f25c78a96d24a8a649b4d to your computer and use it in GitHub Desktop.
How to handle terminal in a safe protected way when cash/interrupt.
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
static unsigned int fid;
static struct termios term;
static char error_stdin[] = "[!] error in /dev/stdin";
static char question_name[] = "[?] what is your secret name:\n";
static char awsner_name_1[] = "[!] ok, good day ";
static char awsner_name_2[] = "!\n";
static char name[256];
static ssize_t len;
int main()
{
fid = open("/dev/stdin", 0);
if (fid == -1) {
write(2, error_stdin, sizeof(error_stdin));
return 15;
}
tcgetattr(fid, &term);
//term.c_lflag &= ~ICANON;
term.c_lflag &= ~ECHO;
term.c_cc[VTIME] = 0;
term.c_cc[VMIN] = 1;
if(tcsetattr(fid, TCSANOW, &term)){
write(2, error_stdin, sizeof(error_stdin));
return 15;
}
write(1, question_name, sizeof(question_name));
len = read(fid, name, sizeof(name));
write(1, awsner_name_1, sizeof(awsner_name_1) - 1);
write(1, name, (len && len < sizeof(name))? --len: sizeof(name));
write(1, awsner_name_2, sizeof(awsner_name_2));
return 0;
}
@RodrigoDornelles
Copy link
Author

use /proc/self/fd/0 instead of /dev/stdin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment