Initial import of termios/echo_off.c
[eleutheria.git] / termios / echo_off.c
blobd4953a76da8f588f8edbfabfe8675e6489277c0b
1 #include <fcntl.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <termios.h>
6 #define PASSLEN 64
8 /* function prototypes */
9 void diep(const char *s);
11 int main(int argc, char *argv[])
13 struct termios oldt, newt;
14 char password[PASSLEN];
15 int fd;
17 /* check argument count */
18 if (argc != 2) {
19 fprintf(stderr, "Usage: %s tty\n", argv[0]);
20 exit(EXIT_FAILURE);
23 /* open terminal device */
24 if ((fd = open(argv[1], O_RDONLY | O_NOCTTY) == -1))
25 diep("open");
27 /* get current termios structure */
28 if (tcgetattr(fd, &oldt) == -1)
29 diep("tcgetattr");
31 /* set new termios structure */
32 newt = oldt;
33 newt.c_lflag &= ~ECHO; /* disable echoing */
34 newt.c_lflag |= ECHONL; /* echo NL even if ECHO is off */
36 if (tcsetattr(fd, TCSANOW, &newt) == -1)
37 diep("tcsetattr");
39 /* prompt for password and get it*/
40 printf("Password: ");
41 scanf("%64s", password);
43 /* restole old termios structure */
44 if (tcsetattr(fd, TCSANOW, &oldt) == -1)
45 diep("tcsetattr");
47 return EXIT_SUCCESS;
50 void diep(const char *s) {
51 perror(s);
52 exit(EXIT_FAILURE);