From 21396186ea84fee55a07f47baff3faeb61723143 Mon Sep 17 00:00:00 2001 From: Stathis Kamperis Date: Mon, 6 Aug 2007 15:12:33 +0300 Subject: [PATCH] Initial import of termios/echo_off.c --- termios/echo_off.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 termios/echo_off.c diff --git a/termios/echo_off.c b/termios/echo_off.c new file mode 100644 index 0000000..d4953a7 --- /dev/null +++ b/termios/echo_off.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#define PASSLEN 64 + +/* function prototypes */ +void diep(const char *s); + +int main(int argc, char *argv[]) +{ + struct termios oldt, newt; + char password[PASSLEN]; + int fd; + + /* check argument count */ + if (argc != 2) { + fprintf(stderr, "Usage: %s tty\n", argv[0]); + exit(EXIT_FAILURE); + } + + /* open terminal device */ + if ((fd = open(argv[1], O_RDONLY | O_NOCTTY) == -1)) + diep("open"); + + /* get current termios structure */ + if (tcgetattr(fd, &oldt) == -1) + diep("tcgetattr"); + + /* set new termios structure */ + newt = oldt; + newt.c_lflag &= ~ECHO; /* disable echoing */ + newt.c_lflag |= ECHONL; /* echo NL even if ECHO is off */ + + if (tcsetattr(fd, TCSANOW, &newt) == -1) + diep("tcsetattr"); + + /* prompt for password and get it*/ + printf("Password: "); + scanf("%64s", password); + + /* restole old termios structure */ + if (tcsetattr(fd, TCSANOW, &oldt) == -1) + diep("tcsetattr"); + + return EXIT_SUCCESS; +} + +void diep(const char *s) { + perror(s); + exit(EXIT_FAILURE); +} -- 2.11.4.GIT