Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
stdin.cpp
Go to the documentation of this file.
1// Copyright (c) 2018 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)
6#include <config/bitcoin-config.h>
7#endif
8
9#include <cstdio> // for fileno(), stdin
10
11#ifdef WIN32
12#include <io.h> // for isatty()
13#include <windows.h> // for SetStdinEcho()
14#else
15#include <poll.h> // for StdinReady()
16#include <termios.h> // for SetStdinEcho()
17#include <unistd.h> // for SetStdinEcho(), isatty()
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#ifdef WIN32
26 DWORD mode;
27 GetConsoleMode(hStdin, &mode);
28 if (!enable) {
29 mode &= ~ENABLE_ECHO_INPUT;
30 } else {
31 mode |= ENABLE_ECHO_INPUT;
32 }
34#else
35 struct termios tty;
37 if (!enable) {
38 tty.c_lflag &= ~ECHO;
39 } else {
40 tty.c_lflag |= ECHO;
41 }
43#endif
44}
45
47#ifdef WIN32
48 return _isatty(_fileno(stdin));
49#else
50 return isatty(fileno(stdin));
51#endif
52}
53
54bool StdinReady() {
55 if (!StdinTerminal()) {
56 return true;
57 }
58#ifdef WIN32
59 return false;
60#else
61 struct pollfd fds;
62 fds.fd = 0; /* this is STDIN */
63 fds.events = POLLIN;
64 return poll(&fds, 1, 0) == 1;
65#endif
66}
67
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
bool StdinReady()
Definition stdin.cpp:54
void SetStdinEcho(bool enable)
Definition stdin.cpp:23
bool StdinTerminal()
Definition stdin.cpp:46
~NoechoInst()
Definition stdin.cpp:71
NoechoInst()
Definition stdin.cpp:68