-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneonate.c
More file actions
119 lines (100 loc) · 3.32 KB
/
neonate.c
File metadata and controls
119 lines (100 loc) · 3.32 KB
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
#include "headers.h"
int print_pid_of_latest_process_in_interval(char** argument_tokens) {
if (argument_tokens[1] == NULL) {
fprintf(stderr, "\033[1;31mneonate: Invalid argument\033[1;0m\n");
return 0;
} else {
if (argument_tokens[1][0] == '-' && strcmp(argument_tokens[1], "-n") != 0) {
fprintf(stderr, "\033[1;31mneonate : Invalid flag: must be -n\033[1;0m\n");
return 0;
}
if (strcmp(argument_tokens[1], "-n") != 0) {
fprintf(stderr, "\033[1;31mneonate : Invalid argument (-n missing)\033[1;0m\n");
return 0;
} else {
if (argument_tokens[2] == NULL) {
fprintf(stderr, "\033[1;31mneonate : missing argument (time)\033[1;0m\n");
return 0;
} else {
int t_sec = atoi(argument_tokens[2]);
return neonate(t_sec);
}
}
}
return 1;
}
void die(const char *s) {
perror(s);
return;
}
struct termios orig_termios;
void disableRawMode() {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) == -1)
die("tcsetattr");
}
void enableRawMode() {
// tcgetattr() gets the curretn terminal settings and we store it in a temporary variable (used to reset when raw mode is turned off)
if (tcgetattr(STDIN_FILENO, &orig_termios) == -1) die("tcgetattr");
atexit(disableRawMode);
struct termios raw = orig_termios;
raw.c_lflag &= ~(ICANON | ECHO);
// Set minimum number of characters for non-blocking read
raw.c_cc[VMIN] = 0;
// Set a timeout for non-blocking read (in deciseconds)
raw.c_cc[VTIME] = 1;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw) == -1) die("tcsetattr");
}
int neonate(int time_in_seconds) {
if (time_in_seconds <= 0) {
fprintf(stderr, "\033[1;31mneonate: Invalid time in seconds - should be a positive integer\033[1;0m\n");
return 0;
}
char path_loadavg[] = "/proc/loadavg";
char *inp = malloc(sizeof(char) * 100);
char c;
enableRawMode();
while (1) {
char read_buff[MAX_LEN];
int fd = open(path_loadavg, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "\033[1;31mopen: could not open file\033[1;0m\n");
return 0;
}
if (read(fd, read_buff, MAX_LEN - 1) < 0) {
fprintf(stderr, "\033[1;31mread: could not read from the file\033[1;0m\n");
close(fd);
return 0;
}
close(fd);
char** fields = generate_tokens(read_buff, ' ');
// 5th field is the pid of the latest created process
int pid_latest = atoi(fields[4]);
free_tokens(fields);
setbuf(stdout, NULL);
printf("%d\n", pid_latest);
memset(inp, '\0', 100);
int pt = 0;
int start_time = time(NULL);
int flag = 0;
while(1) {
if (read(STDIN_FILENO, &c, 1) == 1) {
if (c == 'x') {
flag = 1;
break;
}
}
int curr_time = time(NULL) - start_time;
if (curr_time < time_in_seconds) {
continue;
} else {
break;
}
}
if (flag == 1) {
break;
}
}
disableRawMode();
free(inp);
return 1;
}