-
Notifications
You must be signed in to change notification settings - Fork 0
/
neonate.c
57 lines (49 loc) · 1.33 KB
/
neonate.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
#include "neonate.h"
void neonate(char **str)
{
char chr;
int count = 0;
struct termios original_termios;
tcgetattr(STDIN_FILENO, &original_termios);
struct termios raw_termios = original_termios;
raw_termios.c_lflag &= ~(ICANON | ECHO);
raw_termios.c_cc[VMIN] = 0;
raw_termios.c_cc[VTIME] = 1;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw_termios);
time_t start_time;
time_t end_time;
time(&start_time);
while (1)
{
time(&end_time);
double spent_time = difftime(end_time, start_time);
if (spent_time > (count * atoi(str[2])))
{
count++;
FILE *fptr = fopen("/proc/loadavg", "r");
if (fptr == NULL)
{
fprintf(stderr, "Can't open /proc/loadavg file\n");
break;
}
char info[1024];
fgets(info, 1024, fptr);
char *value = strtok(info, " ");
for (int i = 0; i < 4; i++)
value = strtok(NULL, " ");
printf("%s", value);
}
if (read(STDIN_FILENO, &chr, 1) == -1)
{
perror("read");
break;
}
if (chr == 'x')
{
chr = ' ';
break;
}
}
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
return;
}