-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjobs.c
79 lines (66 loc) · 1.8 KB
/
jobs.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
#include "shell.h"
#include "utils.h"
#include "process.h"
#include "jobs.h"
char get_proc_state(pid_t pid) {
// gets process state from appropriate file
char* stat_path = (char*)malloc(sizeof(char) * (15 + length_num(pid)));
sprintf(stat_path, "/proc/%d/status", pid);
FILE* f = fopen(stat_path, "r");
if (f == NULL) {
perror("Could not read process state");
free(stat_path);
return '\0';
}
char* temp = (char*)malloc(sizeof(char) * MAX_STATIC_STR_LEN);
char p_status;
int i = 0;
while (fgets(temp, MAX_STATIC_STR_LEN - 1, f) != NULL) {
i++;
if (i == 3) {
// Status
strtok(temp, space_delim);
char* t = strtok(NULL, space_delim);
if (t == NULL) {
fprintf(stderr, "Could not read process state: Data unavailable.\n");
p_status = '\0';
} else {
p_status = t[0];
}
break;
}
}
free(stat_path);
free(temp);
fclose(f);
return p_status;
}
void handle_jobs(int argc, char** argv) {
if (argc > 0 && strcmp(argv[argc-1], "&") == 0) {
argc--;
}
if (argc != 0) {
fprintf(stderr, "Could not list jobs: Invalid arguments.\n");
return;
}
jobs();
}
bool jobs() {
node* cur = processes;
int i = 0;
while (cur != NULL) {
i++;
printf("[%d] ", i);
char p_state = get_proc_state(cur->data.pid);
if (p_state == '\0') {
return false; // error printed by get_proc_state
} else if (p_state == 'T') {
printf("Stopped ");
} else {
printf("Running ");
}
printf("%s %d\n", cur->data.pname, cur->data.pid);
cur = cur->next;
}
return true;
}