-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_nightswatch.c
138 lines (107 loc) · 2.92 KB
/
shell_nightswatch.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
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
#include "shell_nightswatch.h"
int nightswatch(char** args, char* root) {
int args_len = 0;
int i = 0;
while(args[i] != NULL) {
args_len++;
i++;
}
int start_time = time(NULL);
if (args_len == 1) {
fprintf(stderr, "nightswatch: Use nightswatch [options] <valid command>\nHere valid commands are 'interrupt' and 'dirty'\n");
return -1;
}
int num_secs = 2;
int opt_index = 0;
if (args[1] != NULL && strcmp(args[1], "-n") == 0) {
num_secs = atoi(args[2]);
opt_index = 3;
}
else {
opt_index = 1;
}
if (args[opt_index] != NULL) {
printf("%s\n", args[opt_index]);
WINDOW* curr = initscr();
WINDOW * win;
int flag_interrupt = 0, flag_dirty = 0;
if (strcmp(args[opt_index], "interrupt") == 0) {
flag_interrupt = 1;
}
if (strcmp(args[opt_index], "dirty") == 0) {
flag_dirty = 1;
}
win = newwin(1280,720,1,1);
noecho();
keypad(win, TRUE);
curs_set(0);
nodelay(win,1);
int start_time = time(NULL) , prev_time = time(NULL);
size_t cpu_size;
char *buffer;
char* heading = (char*)malloc(200);
char* file_path;
if (flag_interrupt == 1) {
sprintf(heading, "NIGHTSWATCH INTERRUPT: %ds\n", num_secs);
file_path = (char*)malloc(sizeof("/proc/interrupts"));
strcpy(file_path, "/proc/interrupts");
}
if (flag_dirty == 1) {
sprintf(heading, "NIGHTSWATCH DIRTY: %ds\n", num_secs);
file_path = (char*)malloc(sizeof("/proc/meminfo"));
strcpy(file_path, "/proc/meminfo");
}
int line_nums = 5;
char *cpuinfo;
FILE* fd = fopen(file_path,"r");
getline(&cpuinfo, &cpu_size, fd);
fclose(fd);
mvwaddstr(win, 1, 5, heading);
mvwaddstr(win, 3, 5,cpuinfo);
for (int z = 0;;z++)
{
int current_time = time(NULL);
if((current_time - start_time) % num_secs == 0 && current_time != prev_time)
{
char *data;
FILE* fd1 = fopen(file_path, "r");
prev_time = current_time;
line_nums++;
size_t nowbufsize = 0;
fseek(fd1, 0, SEEK_SET);
// getline(&data, &nowbufsize, fd1);
// getline(&data, &nowbufsize, fd1);
int m = 0;
int times = 0;
if (flag_interrupt == 1)
times = 3;
else if (flag_dirty == 1)
times = 17;
while (m < times) {
getline(&data, &nowbufsize, fd1);
m++;
}
mvwaddstr(win, line_nums, 5, data);
fclose(fd1);
}
if(line_nums > 30) {
line_nums = 5;
}
mvwaddstr(win, 1, 5, heading);
mvwaddstr(win, 3, 5,cpuinfo);
wrefresh(win);
if(wgetch(win) == 'q') {
break;
}
}
noecho();
curs_set(1);
delwin(win);
endwin();
return 1;
}
else {
fprintf(stderr, "nightswatch: Use nightswatch [options] <valid command>\nHere valid commands are 'interrupt' and 'dirty'\n");
return -1;
}
}