Bitcoin ABC
0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
src
compat
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
23
void
SetStdinEcho
(
bool
enable
) {
24
#ifdef WIN32
25
HANDLE
hStdin
=
GetStdHandle
(
STD_INPUT_HANDLE
);
26
DWORD
mode;
27
GetConsoleMode
(
hStdin
, &mode);
28
if
(!
enable
) {
29
mode &=
~ENABLE_ECHO_INPUT
;
30
}
else
{
31
mode |=
ENABLE_ECHO_INPUT
;
32
}
33
SetConsoleMode
(
hStdin
, mode);
34
#else
35
struct
termios
tty
;
36
tcgetattr
(
STDIN_FILENO
, &
tty
);
37
if
(!
enable
) {
38
tty
.c_lflag &=
~ECHO
;
39
}
else
{
40
tty
.c_lflag |=
ECHO
;
41
}
42
(
void
)
tcsetattr
(
STDIN_FILENO
,
TCSANOW
, &
tty
);
43
#endif
44
}
45
46
bool
StdinTerminal
() {
47
#ifdef WIN32
48
return
_isatty
(
_fileno
(
stdin
));
49
#else
50
return
isatty
(
fileno
(
stdin
));
51
#endif
52
}
53
54
bool
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
68
NoechoInst::NoechoInst
() {
69
SetStdinEcho
(
false
);
70
}
71
NoechoInst::~NoechoInst
() {
72
SetStdinEcho
(
true
);
73
}
GetRand
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
StdinReady
bool StdinReady()
Definition
stdin.cpp:54
SetStdinEcho
void SetStdinEcho(bool enable)
Definition
stdin.cpp:23
StdinTerminal
bool StdinTerminal()
Definition
stdin.cpp:46
stdin.h
NoechoInst::~NoechoInst
~NoechoInst()
Definition
stdin.cpp:71
NoechoInst::NoechoInst
NoechoInst()
Definition
stdin.cpp:68
Generated on Sat Nov 23 2024 02:37:58 for Bitcoin ABC by
1.9.8