-
Notifications
You must be signed in to change notification settings - Fork 0
/
neonate.c
61 lines (44 loc) · 1.25 KB
/
neonate.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
52
53
54
55
56
57
58
59
60
61
#include "header.h"
int orig_flags;
struct termios orig_termios;
void disableRawMode(){
tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios);
fcntl(STDIN_FILENO, F_SETFL, orig_flags);
}
void enableRawMode(){
tcgetattr(STDIN_FILENO, &orig_termios);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ECHO | ICANON);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
}
void setNonBlockingInput(){
orig_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, orig_flags | O_NONBLOCK);
}
void neonate(char* command){
if(strlen(command) <= 11) return;
int frequency = atoi(command+11);
time_t lastrun = 0;
char c;
enableRawMode();
setNonBlockingInput();
while(true){
if(read(STDIN_FILENO, &c, 1) == 1){
if(c == 'x'){
disableRawMode();
break;
}
}
time_t cur_time = time(0);
if((int)difftime(cur_time,lastrun) >= frequency){
FILE *fptr = fopen("/proc/loadavg","r");
if(fptr == NULL) return;
int pid;
fscanf(fptr,"%*s %*s %*s %*s %d",&pid);
printf("%d\n",pid);
fclose(fptr);
lastrun = cur_time;
}
usleep(10000);
}
}