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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>
static const char txtheader[] =
"**************\n"
"* dummyshell *\n"
"**************\n";
int main(int argc, char** argv)
{
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
static struct termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
printf("%s\n", txtheader);
fd_set fds;
time_t start = time(NULL);
time_t cur;
while (1) {
cur = time(NULL);
double diff = difftime(cur, start);
printf("\r( %0.fs ) [PRESS ANY KEY TO QUIT] ", diff);
fflush(stdout);
fflush(stdin);
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
int ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv);
if (ret == 0) {
tv.tv_sec = 1;
tv.tv_usec = 0;
} else {
printf("quit in 3 .. ");
fflush(stdout);
sleep(1);
printf("2 .. ");
fflush(stdout);
sleep(1);
printf("1 .. ");
fflush(stdout);
sleep(1);
printf("\n");
break;
}
if (FD_ISSET(STDIN_FILENO,&fds)) break;
}
while (getchar() != EOF) {}
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
return 0;
}
|