DEBUG symbol is now MPOOL_DEBUG
[eleutheria.git] / termios / echo_off.c
blobe8a28814d55ec67c5aa838de9d27fe89e2d1d9dd
1 /* compile with:
2 gcc echo_off -o echo_off -Wall -W -Wextra -ansi -pedantic
4 Whenever a user types in a password, it is desirable that
5 the password itself doesn't show up at all. To implement
6 such behaviour, we use the termios(4) interface to disable
7 echo'ing.
8 */
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <termios.h>
15 #define PASSLEN 64
17 /* function prototypes */
18 void diep(const char *s);
20 int main(int argc, char *argv[])
22 struct termios oldt, newt;
23 char password[PASSLEN];
24 int fd;
26 /* check argument count */
27 if (argc != 2) {
28 fprintf(stderr, "Usage: %s tty\n", argv[0]);
29 exit(EXIT_FAILURE);
32 /* open terminal device */
33 if ((fd = open(argv[1], O_RDONLY | O_NOCTTY) == -1))
34 diep("open");
36 /* get current termios structure */
37 if (tcgetattr(fd, &oldt) == -1)
38 diep("tcgetattr");
40 /* set new termios structure */
41 newt = oldt;
42 newt.c_lflag &= ~ECHO; /* disable echoing */
43 newt.c_lflag |= ECHONL; /* echo NL even if ECHO is off */
45 if (tcsetattr(fd, TCSANOW, &newt) == -1)
46 diep("tcsetattr");
48 /* prompt for password and get it */
49 printf("Password: ");
50 fgets(password, PASSLEN, stdin);
52 /* restore old termios structure */
53 if (tcsetattr(fd, TCSANOW, &oldt) == -1)
54 diep("tcsetattr");
56 return EXIT_SUCCESS;
59 void diep(const char *s)
61 perror(s);
62 exit(EXIT_FAILURE);