ash,hush: add TODO for rare build failure
[oweals/busybox.git] / libbb / bb_askpass.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Ask for a password
4  * I use a static buffer in this function.  Plan accordingly.
5  *
6  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include "libbb.h"
12
13 /* do nothing signal handler */
14 static void askpass_timeout(int UNUSED_PARAM ignore)
15 {
16 }
17
18 char* FAST_FUNC bb_askpass(int timeout, const char *prompt)
19 {
20         /* Was static char[BIGNUM] */
21         enum { sizeof_passwd = 128 };
22         static char *passwd;
23
24         char *ret;
25         int i;
26         struct sigaction sa, oldsa;
27         struct termios tio, oldtio;
28
29         if (!passwd)
30                 passwd = xmalloc(sizeof_passwd);
31         memset(passwd, 0, sizeof_passwd);
32
33         tcgetattr(STDIN_FILENO, &oldtio);
34         tcflush(STDIN_FILENO, TCIFLUSH);
35         tio = oldtio;
36         tio.c_iflag &= ~(IUCLC|IXON|IXOFF|IXANY);
37         tio.c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|TOSTOP);
38         tcsetattr_stdin_TCSANOW(&tio);
39
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);
45         if (timeout) {
46                 sigaction_set(SIGALRM, &sa);
47                 alarm(timeout);
48         }
49
50         fputs(prompt, stdout);
51         fflush(stdout);
52         ret = NULL;
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) {
56                 ret = passwd;
57                 i = 0;
58                 /* Last byte is guaranteed to be 0
59                    (read did not overwrite it) */
60                 do {
61                         if (passwd[i] == '\r' || passwd[i] == '\n')
62                                 passwd[i] = '\0';
63                 } while (passwd[i++]);
64         }
65
66         if (timeout) {
67                 alarm(0);
68         }
69         sigaction_set(SIGINT, &oldsa);
70
71         tcsetattr_stdin_TCSANOW(&oldtio);
72         bb_putchar('\n');
73         fflush(stdout);
74         return ret;
75 }