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 *bb_askpass(int timeout, const char * prompt)
22 /* Was static char[BIGNUM] */
23 enum { sizeof_passwd = 128 };
29 struct termios old, new;
32 passwd = xmalloc(sizeof_passwd);
33 memset(passwd, 0, sizeof_passwd);
35 tcgetattr(STDIN_FILENO, &old);
36 tcflush(STDIN_FILENO, TCIFLUSH);
38 fputs(prompt, stdout);
41 tcgetattr(STDIN_FILENO, &new);
42 new.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
43 new.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
44 tcsetattr(STDIN_FILENO, TCSANOW, &new);
48 sa.sa_handler = askpass_timeout;
49 sigaction(SIGALRM, &sa, NULL);
54 /* On timeout, read will hopefully be interrupted by SIGALRM,
55 * and we return NULL */
56 if (read(STDIN_FILENO, passwd, sizeof_passwd-1) > 0) {
59 /* Last byte is guaranteed to be 0
60 (read did not overwrite it) */
62 if (passwd[i] == '\r' || passwd[i] == '\n')
64 } while (passwd[i++]);
71 tcsetattr(STDIN_FILENO, TCSANOW, &old);