Bitcoin Core 22.99.0
P2P Digital Currency
stdin.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2019 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#if defined(HAVE_CONFIG_H)
7#endif
8
9#include <cstdio> // for fileno(), stdin
10
11#ifdef WIN32
12#include <windows.h> // for SetStdinEcho()
13#include <io.h> // for isatty()
14#else
15#include <termios.h> // for SetStdinEcho()
16#include <unistd.h> // for SetStdinEcho(), isatty()
17#include <poll.h> // for StdinReady()
18#endif
19
20#include <compat/stdin.h>
21
22// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
23void SetStdinEcho(bool enable)
24{
25#ifdef WIN32
26 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
27 DWORD mode;
28 GetConsoleMode(hStdin, &mode);
29 if (!enable) {
30 mode &= ~ENABLE_ECHO_INPUT;
31 } else {
32 mode |= ENABLE_ECHO_INPUT;
33 }
34 SetConsoleMode(hStdin, mode);
35#else
36 struct termios tty;
37 tcgetattr(STDIN_FILENO, &tty);
38 if (!enable) {
39 tty.c_lflag &= ~ECHO;
40 } else {
41 tty.c_lflag |= ECHO;
42 }
43 (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty);
44#endif
45}
46
48{
49#ifdef WIN32
50 return _isatty(_fileno(stdin));
51#else
52 return isatty(fileno(stdin));
53#endif
54}
55
57{
58 if (!StdinTerminal()) {
59 return true;
60 }
61#ifdef WIN32
62 return false;
63#else
64 struct pollfd fds;
65 fds.fd = 0; /* this is STDIN */
66 fds.events = POLLIN;
67 return poll(&fds, 1, 0) == 1;
68#endif
69}
70
bool StdinReady()
Definition: stdin.cpp:56
void SetStdinEcho(bool enable)
Definition: stdin.cpp:23
bool StdinTerminal()
Definition: stdin.cpp:47
~NoechoInst()
Definition: stdin.cpp:72
NoechoInst()
Definition: stdin.cpp:71