1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
11 /* do nothing signal handler */
12 static void askpass_timeout(int UNUSED_PARAM ignore)
16 char* FAST_FUNC bb_ask_noecho_stdin(const char *prompt)
18 return bb_ask_noecho(STDIN_FILENO, 0, prompt);
20 char* FAST_FUNC bb_ask_noecho(const int fd, int timeout, const char *prompt)
22 /* Was static char[BIGNUM] */
23 enum { sizeof_passwd = 128 };
28 struct sigaction sa, oldsa;
29 struct termios tio, oldtio;
31 tcflush(fd, TCIFLUSH);
32 /* Was buggy: was printing prompt *before* flushing input,
33 * which was upsetting "expect" based scripts of some users.
35 fputs(prompt, stdout);
38 tcgetattr(fd, &oldtio);
41 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL);
42 tcsetattr(fd, TCSANOW, &tio);
44 memset(&sa, 0, sizeof(sa));
45 /* sa.sa_flags = 0; - no SA_RESTART! */
46 /* SIGINT and SIGALRM will interrupt reads below */
47 sa.sa_handler = askpass_timeout;
48 sigaction(SIGINT, &sa, &oldsa);
50 sigaction_set(SIGALRM, &sa);
54 passwd = auto_string(xmalloc(sizeof_passwd));
58 int r = read(fd, &ret[i], 1);
59 if ((i == 0 && r == 0) /* EOF (^D) with no password */
60 || r < 0 /* read is interrupted by timeout or ^C */
66 || ret[i] == '\r' || ret[i] == '\n' /* EOL */
67 || ++i == sizeof_passwd-1 /* line limit */
77 sigaction_set(SIGINT, &oldsa);
78 tcsetattr(fd, TCSANOW, &oldtio);