-
Notifications
You must be signed in to change notification settings - Fork 0
/
nightswatch.c
186 lines (169 loc) · 4.5 KB
/
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "headers.h"
#include "nightswatch.h"
#include <termios.h>
#include <ctype.h>
#include "util.h"
struct termios orig_termios;
void reset_terminal_mode_to_canon() {
tcsetattr(0, TCSANOW, &orig_termios);
}
void set_terminal_raw_mode() {
struct termios new_termios; // terminal attributes
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
atexit(reset_terminal_mode_to_canon); // incase some error occurs
cfmakeraw(&new_termios); // only standard on BSD systems, flips the ICANON AND ECHO flag
// new_termios.c_iflag &= ~(ICRNL | IXON); //control key
// new_termios.c_oflag &= ~(OPOST); // \n -> \r turn off output processing
// new_termios.c_lflag &= ~(ECHO | ICANON); // turn off echo and canonical mode in terminal
tcsetattr(0, TCSANOW, &new_termios);
}
int getch() {
int r;
unsigned char c;
if ((r = read(0, &c, sizeof(c))) < 0) {
return r;
} else {
return c;
}
}
void cpu_header() {
FILE *f;
f = fopen("/proc/interrupts", "r");
if (f == NULL)
return;
char *words[10000];
int n = 0;
//perror("ff");
if (f != NULL) {
words[n] = malloc(size_buff);
while (fscanf(f, "%s ", words[n]) != -1) {
words[++n] = malloc(size_buff);
}
}
// get cpu cores
for (int i = 0; i < n; i++) {
if (isdigit(words[i][0])) {
break;
} else {
char a[100];
sprintf(a, "%10s ", words[i]);
write(1, a, strlen(a));
}
}
write(1, "\n", 2);
fclose(f);
for (int i = 0; i <= n; i++)
free(words[i]);
}
void cpu() {
FILE *f;
f = fopen("/proc/interrupts", "r");
if (f == NULL)
return;
char *words[10000];
int n = 0;
//perror("ff");
if (f != NULL) {
words[n] = malloc(size_buff);
while (fscanf(f, " \t%s", words[n]) != -1) {
words[++n] = malloc(size_buff);
}
}
for (int i = 0; i < n; i++) {
//printf("-%s-\n", words[i]);
if (strcmp(words[i], "1:") == 0) {
for (int j = i + 1;; j++) {
if (!(words[j][0] >= '0' && words[j][0] <= '9')) {
break;
} else {
char a[100];
sprintf(a, "%10s", words[j]);
write(1, a, strlen(a));
}
}
break;
}
}
fclose(f);
write(1, "\n", 2);
for (int i = 0; i <= n; i++)
free(words[i]);
}
void new_born() {
FILE *f = fopen("/proc/loadavg", "r");
if (f == NULL) {
perror("not found");
exit_code = 1;
}
char no[100];
for (int i = 0; i < 5; i++) {
fscanf(f, "%s\n", no);
}
char pr[1000];
sprintf(pr, "%s\n", no);
write(1, pr, strlen(pr));
fclose(f);
}
int kbhit() {
// has a key been pressed or what
struct timeval tv = {0L, 0L};
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}
void nightswatch_handler(char *tokens[], int no) {
if (no != 4) {
fprintf(stderr, "2 args required : -n <int> <command>\n");
exit_code = 1;
return;
}
int seconds;
if (strcmp(tokens[1], "-n") != 0) {
printf("2 args required : -n <int> <command>\n");
exit_code = 1;
}
seconds = (int) strtol(tokens[2], NULL, 10);
if (seconds <= 0) {
fprintf(stderr, "n > 0\n");
}
//printf("%d", seconds);
char *func = tokens[3];
if (strcmp(func, "interrupt") == 0) {
cpu_header();
}
while (true) {
if (strcmp(func, "interrupt") == 0) {
cpu();
} else if (strcmp(func, "newborn") == 0) {
new_born();
} else {
fprintf(stderr, "nightswatch : invalid command \n");
exit_code = 1;
break;
}
set_terminal_raw_mode();
time_t secs = seconds;
time_t startTime = time(NULL);
while (time(NULL) - startTime < secs) {
if (kbhit()) {
if (getch() == 'q') {
reset_terminal_mode_to_canon();
return;
}
}
}
//sleep(seconds);
if (kbhit()) {
if (getch() == 'q') {
reset_terminal_mode_to_canon();
return;
}
}
reset_terminal_mode_to_canon();
fflush(stdout);
}
}
// nightswatch -1 interrupt
// nightswatch -1 newborn