2 * For license terms, see the file COPYING in this directory.
5 /***********************************************************************
8 programmer: Carl Harris, ceharris@mal.com
9 description: getpass() replacement which allows for long passwords.
11 ***********************************************************************/
19 #define INPUT_BUF_SIZE MAX_PASSWORD_LENGTH
21 #if defined(HAVE_TERMIOS_H) && defined(HAVE_TCSETATTR)
24 #if defined(HAVE_TERMIO_H)
25 # include <sys/ioctl.h>
28 #if defined(HAVE_SGTTY_H)
36 #if defined(HAVE_TCSETATTR)
37 static struct termios termb;
38 static tcflag_t flags;
40 #if defined(HAVE_TERMIO_H)
41 static struct termio termb;
42 static unsigned short flags;
44 #if defined(HAVE_STTY)
45 static struct sgttyb ttyb;
51 void save_tty_state();
52 void disable_tty_echo();
53 void restore_tty_state();
60 #if !(defined(HAVE_TCSETATTR) || defined(HAVE_TERMIO_H) || defined(HAVE_STTY))
62 #if defined(HAVE_GETPASS)
64 return getpass(prompt);
66 fputs("ERROR: no support for getpassword() routine\n",stderr);
70 #endif /* !(defined(HAVE_TCSETATTR) || ... */
75 static char pbuf[INPUT_BUF_SIZE];
77 RETSIGTYPE sigint_handler();
80 /* get the file descriptor for the input device */
81 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
84 setbuf(fi, (char *)NULL);
86 /* store descriptor for the tty */
89 /* preserve tty state before turning off echo */
92 /* now that we have the current tty state, we can catch SIGINT and
94 sig = signal(SIGINT, sigint_handler);
96 /* turn off echo on the tty */
99 /* display the prompt and get the input string */
100 fprintf(stderr, "%s", prompt); fflush(stderr);
101 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
102 if (p < &pbuf[INPUT_BUF_SIZE - 1])
107 /* write a newline so cursor won't appear to hang */
108 fprintf(stderr, "\n"); fflush(stderr);
110 /* restore previous state of the tty */
113 /* restore previous state of SIGINT */
127 #if defined(HAVE_TCSETATTR)
128 tcgetattr(ttyfd, &termb);
129 flags = termb.c_lflag;
131 #if defined(HAVE_TERMIO_H)
132 ioctl(ttyfd, TCGETA, (char *) &termb);
133 flags = termb.c_lflag;
134 #else /* we HAVE_STTY */
136 flags = ttyb.sg_flags;
145 /* turn off echo on the tty */
146 #if defined(HAVE_TCSETATTR)
147 termb.c_lflag &= ~ECHO;
148 tcsetattr(ttyfd, TCSAFLUSH, &termb);
150 #if defined(HAVE_TERMIO_H)
151 termb.c_lflag &= ~ECHO;
152 ioctl(ttyfd, TCSETA, (char *) &termb);
153 #else /* we HAVE_STTY */
154 ttyb.sg_flags &= ~ECHO;
165 /* restore previous tty echo state */
166 #if defined(HAVE_TCSETATTR)
167 termb.c_lflag = flags;
168 tcsetattr(ttyfd, TCSAFLUSH, &termb);
170 #if defined(HAVE_TERMIO_H)
171 termb.c_lflag = flags;
172 ioctl(ttyfd, TCSETA, (char *) &termb);
173 #else /* we HAVE_STTY */
174 ttyb.sg_flags = flags;
181 RETSIGTYPE sigint_handler ()
184 fputs("\nCaught signal... bailing out.\n", stderr);