-
Notifications
You must be signed in to change notification settings - Fork 0
/
nightswatch.c
208 lines (169 loc) · 3.97 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "headers.h"
int builtin_nightswatch(char **args, int arg_count)
{
if (arg_count > 4)
{
fprintf(stderr, "Too many arguments\n");
return EXIT_FAILURE;
}
if (arg_count < 2)
{
fprintf(stderr, "Missing arguments\n");
return EXIT_FAILURE;
}
int interval = 5;
int flag = -1; // 0 for interrupt and 1 for newborn
if (strcmp(args[1], "-n") == 0)
{
if (arg_count == 3)
{
fprintf(stderr, "Missing arguments\n");
return EXIT_FAILURE;
}
long num = 0;
char *end = NULL;
num = strtol(args[2], &end, 10);
if (end == args[2])
{
fprintf(stderr, "Not an integer argument\n");
return EXIT_FAILURE;
}
if ((num > INT_MAX) || (num < 0))
{
fprintf(stderr, "Number out of range\n");
return EXIT_FAILURE;
}
interval = (int)num;
if (strcmp(args[3], "interrupt") == 0)
{
flag = 0;
}
else if (strcmp(args[3], "newborn") == 0)
{
flag = 1;
}
}
else
{
if (strcmp(args[1], "interrupt") == 0)
{
flag = 0;
}
else if (strcmp(args[1], "newborn") == 0)
{
flag = 1;
}
}
if (flag == -1)
{
fprintf(stderr, "Agruments interrupt or newborn expected\n");
return EXIT_FAILURE;
}
time_t begin_time = time(NULL);
double count = 0.0;
while (1)
{
time_t cur_time = time(NULL);
if (difftime(cur_time, begin_time) / (double)interval >= count)
{
if (flag == 0)
{
if (interrupt() == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
}
else
{
if (newborn() == EXIT_FAILURE)
{
return EXIT_FAILURE;
}
}
count += 1.0;
}
struct termios orig_termios, new_termios;
tcgetattr(0, &orig_termios);
memcpy(&new_termios, &orig_termios, sizeof(new_termios));
cfmakeraw(&new_termios);
tcsetattr(0, TCSANOW, &new_termios);
if (kbhit() && getchar() == 'q')
{
tcsetattr(0, TCSANOW, &orig_termios);
break;
}
tcsetattr(0, TCSANOW, &orig_termios);
}
return EXIT_SUCCESS;
}
int newborn()
{
FILE *fp = fopen("/proc/loadavg", "r");
if (fp == NULL)
{
perror("fopen() error");
return EXIT_FAILURE;
}
char *line = (char *)malloc(sizeof(char) * BUFFER_SIZE);
if (line == NULL)
{
fprintf(stderr, "Error allocating memory\n");
fclose(fp);
return EXIT_FAILURE;
}
fgets(line, BUFFER_SIZE, fp);
int ind = strlen(line) - 1;
while (ind > 0 && line[ind] != ' ')
{
--ind;
}
fflush(stdout);
printf("%s\n", line + ind + 1);
fclose(fp);
free(line);
return EXIT_SUCCESS;
}
int interrupt()
{
FILE *fp = fopen("/proc/interrupts", "r");
if (fp == NULL)
{
perror("fopen() error");
return EXIT_FAILURE;
}
char *line = (char *)malloc(sizeof(char) * BUFFER_SIZE);
if (line == NULL)
{
fprintf(stderr, "Error allocating memory\n");
fclose(fp);
return EXIT_FAILURE;
}
rewind(fp);
int line_num = 0;
while (fgets(line, BUFFER_SIZE, fp))
{
if (line_num == 0)
{
fflush(stdout);
printf("%s", line + 3);
}
if (line_num == 2)
{
fflush(stdout);
printf("%s\n", line + 6);
break;
}
++line_num;
}
fclose(fp);
free(line);
return EXIT_SUCCESS;
}
int kbhit()
{
struct timeval tv = {0L, 0L};
fd_set fds;
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv);
}