-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnightswatch.c
179 lines (156 loc) · 4.09 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
#include "headers.h"
int delay(int seconds){
// Converting time into milli_seconds
int milli_seconds = 1000 * seconds;
// Storing start time
clock_t start_time = clock();
int okay;
// looping till required time is not achieved
while (clock() < start_time + milli_seconds) {
okay = has_q_pressed();
if(okay){
return 1;
}
}
return 0;
}
int handle_night(char* path[], int n,char* starting_working_directory,char* command){
if(n==2){
if(strcmp(path[1],"interrupt")==0){
nighwatch_interrupt(5);
}
else if(strcmp(path[1],"newborn")==0){
nighwatch_newborn(5);
}
else{
fprintf(stderr,"not a valid command\n");
return 1;
}
}
else if(n==4){
if(strcmp(path[1],"-n")!=0){
fprintf(stderr,"not a right option\n");
return 1;
}
int number = 0;
int number_len = strlen(path[2]);
for(int i=0;i<number_len;i++){
number*=10;
int digit = (int)path[2][i] - (int)'0';
if(digit>9||digit<0){
fprintf(stderr,"not a valid number");
return 1;
}
number+=digit;
// number*=10;
}
printf("%d\n",number);
if(strcmp(path[3],"interrupt")==0){
nighwatch_interrupt(number);
}
else if(strcmp(path[3],"newborn")==0){
nighwatch_newborn(number);
}
else{
fprintf(stderr,"not a valid command\n");
return 1;
}
}
else{
fprintf(stderr,"wrong number of arguments\n");
return 1;
}
// printf("exitted\n");
return 0;
}
int has_q_pressed(){
struct timeval tm;
// set it to a total of 0 so that select does not wait
tm.tv_sec = 0;
tm.tv_usec = 0;
fd_set fds;
// initials it to represent STDIN?
FD_ZERO(&fds);
FD_SET(0, &fds);
int return_val = select(1,&fds,NULL,NULL,&tm);
if(return_val){
char c = fgetc(stdin);
// char d = fgetc(stdin);
if(c=='q'){
return 1;
}
}
else if (return_val==-1){
perror("q_press");
return 1;
}
return 0;
}
int nighwatch_interrupt(int wait){
FILE* f_read = fopen("/proc/interrupts", "r");
if(f_read==NULL){
perror("interrupts");
return 1;
}
char store[1024];
// prints the CPUS
fgets(store, 1000, f_read);
char* words;
words = strtok(store, " ");
while(words!=NULL){
printf("%s\t",words);
words = strtok(NULL, " ");
}
printf("\n");
fclose(f_read);
while(1){
FILE* f_read = fopen("/proc/interrupts", "r");
if(f_read==NULL){
perror("interrupts");
return 1;
}
while (fgets(store, 1000, f_read) != NULL){
words = strtok(store, " ");
if(strcmp(words, "1:") == 0){
words = strtok(NULL, " ");
while(words!=NULL && (words[0]>='0' && words[0]<='9')){
printf("%s\t",words);
words = strtok(NULL, " ");
}
printf("\n");
}
}
fclose(f_read);
int continue_maybe = delay(wait*1000);
if(continue_maybe){
return 0;
}
}
}
int nighwatch_newborn(int wait){
while(1){
FILE* f_read = fopen("/proc/loadavg", "r");
if(f_read==NULL){
perror("interrupts");
return 1;
}
char store[1024];
// prints the CPUS
fgets(store, 1000, f_read);
char* words[10];
words[0] = strtok(store, " ");
int index = 1;
while(words[index-1]!=NULL){
words[index] = strtok(NULL, " ");
index++;
}
// for(int i=0;i<index-1;i++){
// printf("%s ",words[i]);
// }
printf("%s",words[index-2]);
int continue_maybe = delay(wait*1000);
if(continue_maybe){
return 0;
}
}
}