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.
15 /* do nothing signal handler */
16 static void askpass_timeout(int ATTRIBUTE_UNUSED ignore)
20 char* FAST_FUNC bb_askpass(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;
32 passwd = xmalloc(sizeof_passwd);
33 memset(passwd, 0, sizeof_passwd);
35 tcgetattr(STDIN_FILENO, &oldtio);
36 tcflush(STDIN_FILENO, TCIFLUSH);
38 tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
39 tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
40 tcsetattr(STDIN_FILENO, TCSANOW, &tio);
42 memset(&sa, 0, sizeof(sa));
43 /* sa.sa_flags = 0; - no SA_RESTART! */
44 /* SIGINT and SIGALRM will interrupt read below */
45 sa.sa_handler = askpass_timeout;
46 sigaction(SIGINT, &sa, &oldsa);
48 sigaction_set(SIGALRM, &sa);
52 fputs(prompt, stdout);
55 /* On timeout or Ctrl-C, read will hopefully be interrupted,
56 * and we return NULL */
57 if (read(STDIN_FILENO, passwd, sizeof_passwd - 1) > 0) {
60 /* Last byte is guaranteed to be 0
61 (read did not overwrite it) */
63 if (passwd[i] == '\r' || passwd[i] == '\n')
65 } while (passwd[i++]);
71 sigaction_set(SIGINT, &oldsa);
73 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);