-
Notifications
You must be signed in to change notification settings - Fork 0
/
nightswatch.h
140 lines (135 loc) · 2.59 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <curses.h>
void nightswatch(char *argv[])
{
long int waiting;
if (argv[1][0] != '-')
{
waiting = 1;
}
else
{
// Convert string into number
if (argv[1][1] != 'n')
{
printf("Please provide correct flag\n");
waiting = 1;
}
else
{
long int p = 1;
long int num = 0;
for (long int i = 0; i < strlen(argv[2]); i++)
{
num += (argv[2][strlen(argv[2]) - 1 - i] - '0') * p;
p *= 10;
}
waiting = num;
}
}
ll run = 0;
ll mera = -1;
if (!strcmp(argv[3], "interrupts"))
{
mera = 1;
}
else if (!strcmp(argv[3], "dirty"))
{
mera = 2;
}
if(mera==-1){
printf("Invalid\n");
return 0;
}
int pid = fork();
if (pid == 0)
{
int x = 0;
int y = 0;
WINDOW *curr = initscr();
WINDOW* newwindow = newwin(800,600,1,1);
keypad(newwindow,true);
noecho();
nodelay(newwindow,true);
curs_set(0);
int pressed = 0;
int it = 0;
long int curent = time(0);
long int baar = curent;
while (1 && !pressed)
{
baar = time(0);
if(y>80){
y = 0;
}
if(wgetch(newwindow)=='q'){
wclear(newwindow);
break;
}
if((time(0)-curent)==waiting && !pressed && baar!=curent)
{
if(mera == 2)
{
char meminfopath[5000] = "/proc/meminfo";
y++;
curent = time(0);
FILE *fd = fopen(meminfopath, "r");
char u[BUFFER_SIZE];
size_t len;
ll a = fgets(u, BUFFER_SIZE, fd);
ll r = 0;
if (u[0] == 'D' && u[1] == 'i' && u[2] == 'r' && u[3] == 't' && u[4] == 'y')
{
r = 1;
}
while (!r)
{
ll a = fgets(u, BUFFER_SIZE, fd);
if (u[0] == 'D' && u[1] == 'i' && u[2] == 'r' && u[3] == 't' && u[4] == 'y')
{
r = 1;
}
}
mvwaddstr(newwindow,y,0,u);
fclose(fd);
}
else if(mera==1)
{
char interruptpath[5000] = "/proc/interrupts";
int fd = open(interruptpath, O_RDONLY);
int wfd = open("./A.txt",O_CREAT|O_WRONLY,0777);
int a = lseek(fd, 0, SEEK_END);
char u[BUFFER_SIZE];
fgets(u,BUFFER_SIZE,fd );
int cnt = 0;
while (cnt!=2)
{
cnt++;
ll a = fgets(u, BUFFER_SIZE, fd);
}
write(wfd,u,strlen(u));
mvwaddstr(newwindow,y,0,u);
close(fd);
y+=1;
}
wrefresh(newwindow);
}
}
noecho();
curs_set(1);
endwin();
exit(0);
}
else if (pid < 0)
{
perror("Error forking");
}
else
{
int status;
int cc;
;do
{
waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
}