33 #elif defined(_MSC_VER)
71 HANDLE in = GetStdHandle(STD_INPUT_HANDLE);
73 if ((in == INVALID_HANDLE_VALUE)
81 bool is_console = (GetFileType(in) == FILE_TYPE_CHAR);
88 if (GetConsoleMode(in, &flags_save))
90 DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
93 flags |= ENABLE_ECHO_INPUT;
95 SetConsoleMode(in, flags);
107 winput = malloc(capacity *
sizeof(WCHAR));
113 status = ReadConsoleW(in, winput, capacity, &len, NULL);
114 WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL);
119 status = ReadFile(in, input, capacity, &len, NULL);
131 SetConsoleMode(in, flags_save);
144 #ifdef HAVE_TERMIOS_H
157 open_tty(
const bool write)
160 ret = fopen(
"/dev/tty",
write ?
"w" :
"r");
163 ret =
write ? stderr : stdin;
177 if (fp != stderr && fp != stdin)
207 #elif defined(HAVE_TERMIOS_H)
208 bool restore_tty =
false;
209 struct termios tty_tmp, tty_save;
215 if (!isatty(0) && !isatty(2) )
217 int fd = open(
"/dev/tty", O_RDWR );
220 msg(
M_FATAL,
"neither stdin nor stderr are a tty device and you have neither a "
221 "controlling tty nor systemd - can't ask for '%s'. If you used --daemon, "
222 "you need to use --askpass to make passphrase-protected keys work, and you "
223 "can not use --auth-nocache.", prompt );
228 FILE *fp = open_tty(
true);
229 fprintf(fp,
"%s", prompt);
233 fp = open_tty(
false);
235 if (!echo && (tcgetattr(fileno(fp), &tty_tmp) == 0))
238 tty_tmp.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL | ISIG);
239 restore_tty = (tcsetattr(fileno(fp), TCSAFLUSH, &tty_tmp) == 0);
242 if (fgets(input, capacity, fp) != NULL)
250 if (tcsetattr(fileno(fp), TCSAFLUSH, &tty_save) == -1)
264 msg(
M_FATAL,
"Sorry, but I can't get console input on this OS (%s)", prompt);