69 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
71 if ((in == INVALID_HANDLE_VALUE)
79 bool is_console = (GetFileType(in) == FILE_TYPE_CHAR);
86 if (GetConsoleMode(in, &flags_save))
88 DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
91 flags |= ENABLE_ECHO_INPUT;
93 SetConsoleMode(in, flags);
105 winput = malloc(capacity *
sizeof(WCHAR));
111 status = ReadConsoleW(in, winput, capacity, &len, NULL);
112 WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL);
117 status = ReadFile(in, input, capacity, &len, NULL);
129 SetConsoleMode(in, flags_save);
142 #ifdef HAVE_TERMIOS_H
155 open_tty(
const bool write)
158 ret = fopen(
"/dev/tty",
write ?
"w" :
"r");
161 ret =
write ? stderr : stdin;
175 if (fp != stderr && fp != stdin)
205 #elif defined(HAVE_TERMIOS_H)
206 bool restore_tty =
false;
207 struct termios tty_tmp, tty_save;
213 if (!isatty(0) && !isatty(2) )
215 int fd = open(
"/dev/tty", O_RDWR );
218 msg(
M_FATAL,
"neither stdin nor stderr are a tty device and you have neither a "
219 "controlling tty nor systemd - can't ask for '%s'. If you used --daemon, "
220 "you need to use --askpass to make passphrase-protected keys work, and you "
221 "can not use --auth-nocache.", prompt );
226 FILE *fp = open_tty(
true);
227 fprintf(fp,
"%s", prompt);
231 fp = open_tty(
false);
233 if (!echo && (tcgetattr(fileno(fp), &tty_tmp) == 0))
236 tty_tmp.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ISIG);
237 restore_tty = (tcsetattr(fileno(fp), TCSAFLUSH, &tty_tmp) == 0);
240 if (fgets(input, capacity, fp) != NULL)
248 if (tcsetattr(fileno(fp), TCSAFLUSH, &tty_save) == -1)
262 msg(
M_FATAL,
"Sorry, but I can't get console input on this OS (%s)", prompt);