-
Notifications
You must be signed in to change notification settings - Fork 0
/
asig.c
51 lines (41 loc) · 1.06 KB
/
asig.c
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <termios.h>
bool cont = true;
void sigHandler(int sig) {
if (sig == SIGINT)
cont ^= true;
signal(SIGINT, sigHandler); // reinstall handler
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Wrong number of arguments.\n");
return EXIT_FAILURE;
}
signal(SIGINT, sigHandler);
struct termios oldTerm, newTerm;
tcgetattr(1, &oldTerm);
newTerm = oldTerm; // copy old terminal settings
newTerm.c_lflag &= ~ECHO; // then make changes
tcsetattr(1, TCSANOW, &newTerm);
printf("Initiating Countdown.\n");
int num = atoi(argv[1]);
while (true) {
while (cont && num > 0) {
printf("%d\n", num--);
sleep(1);
}
if (num <= 0)
break;
printf("pausing\n");
while (!cont) {
sleep(1);
}
}
tcsetattr(1, TCSANOW, &oldTerm);
printf("Done.\n");
return 0;
}