kexec-tools: Make build of kdump optional
[openwrt.git] / package / ead / src / tinysrp / t_getpass.c
1 /*
2 * Copyright 1990 - 1995, Julianne Frances Haugh
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 */
29
30 #include "t_defines.h"
31 #ifdef _WIN32
32 #include <windows.h>
33 #include <io.h>
34 #endif /* _WIN32 */
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif /* HAVE_UNISTD_H */
38 #include <signal.h>
39 #include <stdio.h>
40
41 static int sig_caught;
42 #ifdef HAVE_SIGACTION
43 static struct sigaction sigact;
44 #endif
45
46 /*ARGSUSED*/
47 static RETSIGTYPE
48 sig_catch (sig)
49 int sig;
50 {
51 sig_caught = 1;
52 }
53
54 _TYPE( int )
55 t_getpass (buf, maxlen, prompt)
56 char *buf;
57 unsigned maxlen;
58 const char *prompt;
59 {
60 char *cp;
61 #ifdef _WIN32
62 HANDLE handle = (HANDLE) _get_osfhandle(_fileno(stdin));
63 DWORD mode;
64
65 GetConsoleMode( handle, &mode );
66 SetConsoleMode( handle, mode & ~ENABLE_ECHO_INPUT );
67
68 if(fputs(prompt, stdout) == EOF ||
69 fgets(buf, maxlen, stdin) == NULL) {
70 SetConsoleMode(handle,mode);
71 return -1;
72 }
73 cp = buf + strlen(buf) - 1;
74 if ( *cp == 0x0a )
75 *cp = '\0';
76 printf("\n");
77 SetConsoleMode(handle,mode);
78 #else
79 FILE *fp;
80 int tty_opened = 0;
81
82 #ifdef HAVE_SIGACTION
83 struct sigaction old_sigact;
84 #else
85 RETSIGTYPE (*old_signal)();
86 #endif
87 TERMIO new_modes;
88 TERMIO old_modes;
89
90 /*
91 * set a flag so the SIGINT signal can be re-sent if it
92 * is caught
93 */
94
95 sig_caught = 0;
96
97 /*
98 * if /dev/tty can't be opened, getpass() needs to read
99 * from stdin instead.
100 */
101
102 if ((fp = fopen ("/dev/tty", "r")) == 0) {
103 fp = stdin;
104 setbuf (fp, (char *) 0);
105 } else {
106 tty_opened = 1;
107 }
108
109 /*
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)
113 */
114
115 if (GTTY (fileno (fp), &new_modes))
116 return -1;
117
118 old_modes = new_modes;
119
120 #ifdef HAVE_SIGACTION
121 sigact.sa_handler = sig_catch;
122 (void) sigaction (SIGINT, &sigact, &old_sigact);
123 #else
124 old_signal = signal (SIGINT, sig_catch);
125 #endif
126
127 #ifdef USE_SGTTY
128 new_modes.sg_flags &= ~ECHO;
129 #else
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;
135 #endif
136
137 if (STTY (fileno (fp), &new_modes))
138 goto out;
139
140 /*
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
144 * returned.
145 */
146
147 if (fputs (prompt, stdout) == EOF)
148 goto out;
149
150 (void) fflush (stdout);
151
152 if (fgets (buf, maxlen, fp) == buf) {
153 if ((cp = strchr (buf, '\n')))
154 *cp = '\0';
155 else
156 buf[maxlen - 1] = '\0';
157
158 #ifdef USE_SGTTY
159 putc ('\n', stdout);
160 #endif
161 }
162 else buf[0] = '\0';
163 out:
164 /*
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.
169 */
170
171 if (STTY (fileno (fp), &old_modes))
172 { memset (buf, 0, maxlen); return -1; }
173
174 #ifdef HAVE_SIGACTION
175 (void) sigaction (SIGINT, &old_sigact, NULL);
176 #else
177 (void) signal (SIGINT, old_signal);
178 #endif
179
180 if (tty_opened)
181 (void) fclose (fp);
182
183 if (sig_caught) {
184 kill (getpid (), SIGINT);
185 memset (buf, 0, maxlen);
186 return -1;
187 }
188 #endif
189
190 return 0;
191 }
This page took 0.05112 seconds and 5 git commands to generate.