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 ***********************************************************************/
17 #if defined(HAVE_UNISTD_H)
23 #define INPUT_BUF_SIZE MAX_PASSWORD_LENGTH
25 #if defined(HAVE_TERMIOS_H) && defined(HAVE_TCSETATTR)
28 #if defined(HAVE_TERMIO_H)
29 # include <sys/ioctl.h>
32 #if defined(HAVE_SGTTY_H)
40 #if defined(HAVE_TCSETATTR)
41 static struct termios termb;
42 static tcflag_t flags;
44 #if defined(HAVE_TERMIO_H)
45 static struct termio termb;
46 static unsigned short flags;
48 #if defined(HAVE_STTY)
49 static struct sgttyb ttyb;
55 void save_tty_state();
56 void disable_tty_echo();
57 void restore_tty_state();
64 #if !(defined(HAVE_TCSETATTR) || defined(HAVE_TERMIO_H) || defined(HAVE_STTY))
66 #if defined(HAVE_GETPASS)
68 return getpass(prompt);
70 fputs("ERROR: no support for getpassword() routine\n",stderr);
74 #endif /* !(defined(HAVE_TCSETATTR) || ... */
79 static char pbuf[INPUT_BUF_SIZE];
81 RETSIGTYPE sigint_handler();
84 /* get the file descriptor for the input device */
85 if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
88 setbuf(fi, (char *)NULL);
90 /* store descriptor for the tty */
93 /* preserve tty state before turning off echo */
96 /* now that we have the current tty state, we can catch SIGINT and
98 sig = signal(SIGINT, sigint_handler);
100 /* turn off echo on the tty */
103 /* display the prompt and get the input string */
104 fprintf(stderr, "%s", prompt); fflush(stderr);
105 for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
106 if (p < &pbuf[INPUT_BUF_SIZE - 1])
111 /* write a newline so cursor won't appear to hang */
112 fprintf(stderr, "\n"); fflush(stderr);
114 /* restore previous state of the tty */
117 /* restore previous state of SIGINT */
131 #if defined(HAVE_TCSETATTR)
132 tcgetattr(ttyfd, &termb);
133 flags = termb.c_lflag;
135 #if defined(HAVE_TERMIO_H)
136 ioctl(ttyfd, TCGETA, (char *) &termb);
137 flags = termb.c_lflag;
138 #else /* we HAVE_STTY */
140 flags = ttyb.sg_flags;
149 /* turn off echo on the tty */
150 #if defined(HAVE_TCSETATTR)
151 termb.c_lflag &= ~ECHO;
152 tcsetattr(ttyfd, TCSAFLUSH, &termb);
154 #if defined(HAVE_TERMIO_H)
155 termb.c_lflag &= ~ECHO;
156 ioctl(ttyfd, TCSETA, (char *) &termb);
157 #else /* we HAVE_STTY */
158 ttyb.sg_flags &= ~ECHO;
169 /* restore previous tty echo state */
170 #if defined(HAVE_TCSETATTR)
171 termb.c_lflag = flags;
172 tcsetattr(ttyfd, TCSAFLUSH, &termb);
174 #if defined(HAVE_TERMIO_H)
175 termb.c_lflag = flags;
176 ioctl(ttyfd, TCSETA, (char *) &termb);
177 #else /* we HAVE_STTY */
178 ttyb.sg_flags = flags;
185 RETSIGTYPE sigint_handler ()
188 fputs("\nCaught signal... bailing out.\n", stderr);