1 /* vi: set sw=4 ts=4: */
4 * I use a static buffer in this function. Plan accordingly.
6 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 /* do nothing signal handler */
14 static void askpass_timeout(int UNUSED_PARAM ignore)
18 char* FAST_FUNC bb_askpass(int timeout, const char *prompt)
20 /* Was static char[BIGNUM] */
21 enum { sizeof_passwd = 128 };
26 struct sigaction sa, oldsa;
27 struct termios tio, oldtio;
30 passwd = xmalloc(sizeof_passwd);
31 memset(passwd, 0, sizeof_passwd);
33 tcgetattr(STDIN_FILENO, &oldtio);
34 tcflush(STDIN_FILENO, TCIFLUSH);
36 tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
37 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
38 tcsetattr_stdin_TCSANOW(&tio);
40 memset(&sa, 0, sizeof(sa));
41 /* sa.sa_flags = 0; - no SA_RESTART! */
42 /* SIGINT and SIGALRM will interrupt read below */
43 sa.sa_handler = askpass_timeout;
44 sigaction(SIGINT, &sa, &oldsa);
46 sigaction_set(SIGALRM, &sa);
50 fputs(prompt, stdout);
53 /* On timeout or Ctrl-C, read will hopefully be interrupted,
54 * and we return NULL */
55 if (read(STDIN_FILENO, passwd, sizeof_passwd - 1) > 0) {
58 /* Last byte is guaranteed to be 0
59 (read did not overwrite it) */
61 if (passwd[i] == '\r' || passwd[i] == '\n')
63 } while (passwd[i++]);
69 sigaction_set(SIGINT, &oldsa);
71 tcsetattr_stdin_TCSANOW(&oldtio);