-
Notifications
You must be signed in to change notification settings - Fork 0
/
nightswatch.h
98 lines (95 loc) · 2.06 KB
/
nightswatch.h
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
void dirty()
{
FILE* file = fopen("/proc/meminfo","r");
int count = 1;
if (file != NULL)
{
char line[100];
while(fgets(line,sizeof line,file) != NULL)
{
if(count == 17)
{
char* token = strtok(line,": ");
token = strtok(NULL,": ");
char* size = strtok(NULL," ");
strcat(token,size);
printf("%s",token);
break;
}
else count++;
}
fclose(file);
}
return;
}
void interrupt()
{
FILE* file = fopen("/proc/interrupts","r");
int count = 1;
if (file != NULL)
{
char line[100];
while(fgets(line,sizeof line,file) != NULL)
{
if(count == 3)
{
printf("%s",line);
break;
}
else count++;
}
fclose(file);
}
return;
}
void nightswatch(int time,char* command)
{
int n;
struct termios old;
struct termios new;
unsigned char key;
if(strcmp(command,"interrupt") != 0 && strcmp(command,"dirty") !=0)
{
fprintf(stderr,"Invalid Command.\n");
return;
}
if(strcmp(command,"interrupt") == 0)
{
FILE* file = fopen("/proc/interrupts","r");
int count = 1;
char line[100];
if (file != NULL)
{
fgets(line,sizeof line,file);
printf("%s",line);
}
}
tcgetattr(0,&old);
new = old;
new.c_lflag &= ~ICANON;
new.c_lflag &= ~ECHO;
new.c_lflag &= ~ISIG;
new.c_cc[VMIN] = 0;
new.c_cc[VTIME] = 0;
tcsetattr(0,TCSANOW,&new);
while(1)
{
n = getchar();
if (n != EOF)
{
key = n;
if(key == 'q')
{
break;
}
}
if(strcmp(command,"interrupt") == 0)
interrupt();
if(strcmp(command,"dirty") == 0)
dirty();
sleep(time);
fflush(stdout);
}
tcsetattr(0,TCSANOW, &old);
return ;
}