-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetch.c
More file actions
39 lines (31 loc) · 687 Bytes
/
Copy pathgetch.c
File metadata and controls
39 lines (31 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
#include "getch.h"
char _getch(void) {
char c = 0;
struct termios old;
struct termios raw;
if (tcgetattr(0, &old) != 0)
return 0;
raw = old;
raw.c_lflag &= (tcflag_t)~(tcflag_t)ICANON;
raw.c_lflag &= (tcflag_t)~(tcflag_t)ECHO;
raw.c_cc[VMIN]=1;
raw.c_cc[VTIME]=0;
if (tcsetattr(0, TCSANOW, &raw) != 0)
return 0;
if (read(0, &c, 1) < 0)
c = 0;
tcsetattr(0, TCSADRAIN, &old);
return c;
}
int _kbhit(void) {
struct timeval tv;
fd_set readfds;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(0, &readfds);
return select(1, &readfds, NULL, NULL, &tv) > 0;
}