1 /* getpass() - read a password Author: Kees J. Bot
15 __weak_alias(getpass
, _getpass
)
20 static void catch(int sig
)
25 char *getpass(const char *prompt
)
27 struct sigaction osa
, sa
;
28 struct termios cooked
, raw
;
29 static char password
[32+1];
32 /* Try to open the controlling terminal. */
33 if ((fd
= open("/dev/tty", O_RDONLY
)) < 0) return NULL
;
35 /* Trap interrupts unless ignored. */
37 sigaction(SIGINT
, NULL
, &osa
);
38 if (osa
.sa_handler
!= SIG_IGN
) {
39 sigemptyset(&sa
.sa_mask
);
42 sigaction(SIGINT
, &sa
, &osa
);
45 /* Set the terminal to non-echo mode. */
46 tcgetattr(fd
, &cooked
);
51 raw
.c_oflag
|= OPOST
| ONLCR
;
52 tcsetattr(fd
, TCSANOW
, &raw
);
54 /* Print the prompt. (After setting non-echo!) */
55 write(2, prompt
, strlen(prompt
));
57 /* Read the password, 32 characters max. */
58 while (read(fd
, password
+n
, 1) > 0) {
59 if (password
[n
] == '\n') break;
64 /* Terminal back to cooked mode. */
65 tcsetattr(fd
, TCSANOW
, &cooked
);
70 sigaction(SIGINT
, &osa
, NULL
);
71 if (intr
) raise(SIGINT
);