Dummy commit to test new ssh key
[eleutheria.git] / termios / echo_off.c
blob8abfd2d1448d0266c29184b5df74d1b1a79585a3
1 /*
2 * Compile with:
3 * gcc echo_off -o echo_off -Wall -W -Wextra -ansi -pedantic
5 * Whenever a user types in a password, it is desirable that
6 * the password itself doesn't show up at all. To implement
7 * such behaviour, we use the termios(4) interface to disable
8 * echo'ing.
9 */
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <termios.h>
16 #define PASSLEN 64
18 /* Function prototypes */
19 void diep(const char *s);
21 int main(int argc, char *argv[])
23 struct termios oldt, newt;
24 char password[PASSLEN];
25 int fd;
27 /* Check argument count */
28 if (argc != 2) {
29 fprintf(stderr, "Usage: %s tty\n", argv[0]);
30 exit(EXIT_FAILURE);
33 /* Open terminal device */
34 if ((fd = open(argv[1], O_RDONLY | O_NOCTTY) == -1))
35 diep("open");
37 /* Get current termios structure */
38 if (tcgetattr(fd, &oldt) == -1)
39 diep("tcgetattr");
41 /* Set new termios structure */
42 newt = oldt;
43 newt.c_lflag &= ~ECHO; /* disable echoing */
44 newt.c_lflag |= ECHONL; /* echo NL even if ECHO is off */
46 if (tcsetattr(fd, TCSANOW, &newt) == -1)
47 diep("tcsetattr");
49 /* Prompt for password and get it */
50 printf("Password: ");
51 fgets(password, PASSLEN, stdin);
53 /* Restore old termios structure */
54 if (tcsetattr(fd, TCSANOW, &oldt) == -1)
55 diep("tcsetattr");
57 return EXIT_SUCCESS;
60 void diep(const char *s)
62 perror(s);
63 exit(EXIT_FAILURE);