2 * Copyright 1990 - 1995, Julianne Frances Haugh
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Julianne F. Haugh nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY JULIE HAUGH AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL JULIE HAUGH OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include "t_defines.h"
37 #endif /* HAVE_UNISTD_H */
41 static int sig_caught
;
43 static struct sigaction sigact
;
55 t_getpass (buf
, maxlen
, prompt
)
62 HANDLE handle
= (HANDLE
) _get_osfhandle(_fileno(stdin
));
65 GetConsoleMode( handle
, &mode
);
66 SetConsoleMode( handle
, mode
& ~ENABLE_ECHO_INPUT
);
68 if(fputs(prompt
, stdout
) == EOF
||
69 fgets(buf
, maxlen
, stdin
) == NULL
) {
70 SetConsoleMode(handle
,mode
);
73 cp
= buf
+ strlen(buf
) - 1;
77 SetConsoleMode(handle
,mode
);
83 struct sigaction old_sigact
;
85 RETSIGTYPE (*old_signal
)();
91 * set a flag so the SIGINT signal can be re-sent if it
98 * if /dev/tty can't be opened, getpass() needs to read
102 if ((fp
= fopen ("/dev/tty", "r")) == 0) {
104 setbuf (fp
, (char *) 0);
110 * the current tty modes must be saved so they can be
111 * restored later on. echo will be turned off, except
112 * for the newline character (BSD has to punt on this)
115 if (GTTY (fileno (fp
), &new_modes
))
118 old_modes
= new_modes
;
120 #ifdef HAVE_SIGACTION
121 sigact
.sa_handler
= sig_catch
;
122 (void) sigaction (SIGINT
, &sigact
, &old_sigact
);
124 old_signal
= signal (SIGINT
, sig_catch
);
128 new_modes
.sg_flags
&= ~ECHO
;
130 new_modes
.c_iflag
&= ~IGNCR
;
131 new_modes
.c_iflag
|= ICRNL
;
132 new_modes
.c_oflag
|= OPOST
|ONLCR
;
133 new_modes
.c_lflag
&= ~(ECHO
|ECHOE
|ECHOK
);
134 new_modes
.c_lflag
|= ICANON
|ECHONL
;
137 if (STTY (fileno (fp
), &new_modes
))
141 * the prompt is output, and the response read without
142 * echoing. the trailing newline must be removed. if
143 * the fgets() returns an error, a NULL pointer is
147 if (fputs (prompt
, stdout
) == EOF
)
150 (void) fflush (stdout
);
152 if (fgets (buf
, maxlen
, fp
) == buf
) {
153 if ((cp
= strchr (buf
, '\n')))
156 buf
[maxlen
- 1] = '\0';
165 * the old SIGINT handler is restored after the tty
166 * modes. then /dev/tty is closed if it was opened in
167 * the beginning. finally, if a signal was caught it
168 * is sent to this process for normal processing.
171 if (STTY (fileno (fp
), &old_modes
))
172 { memset (buf
, 0, maxlen
); return -1; }
174 #ifdef HAVE_SIGACTION
175 (void) sigaction (SIGINT
, &old_sigact
, NULL
);
177 (void) signal (SIGINT
, old_signal
);
184 kill (getpid (), SIGINT
);
185 memset (buf
, 0, maxlen
);
This page took 0.058274 seconds and 5 git commands to generate.