-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobs.c
More file actions
54 lines (46 loc) · 1.45 KB
/
jobs.c
File metadata and controls
54 lines (46 loc) · 1.45 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
#include "headers.h"
void jobs(int argc, char **argv)
{
NodePtr temp = head->next;
if (argc > 2)
{
printf("bash: Too many arguments\n");
return;
}
while (temp != NULL)
{
int job = temp->job, pid = temp->pid, row_num = 0;
char status[MAX], command[MAX], path[MAX], row[MAX];
strcpy(command, temp->name);
FILE *fptr;
sprintf(path, "/proc/%d/stat", pid);
if ((fptr = fopen(path, "r")) != NULL)
{
if (fgets(row, sizeof(row), fptr) == NULL)
{
printf("bash: can't access %s such file or directory\n", path);
return;
}
}
else
return;
char *token = strtok(row, " \t");
while (token != NULL && row_num < 3)
{
row_num++;
if (row_num == 3)
{
if (strcmp(token, "S") == 0 || strcmp(token, "R")==0|| strcmp(token, "D")==0)
{
strcpy(status, "Running");
}
else
strcpy(status, "Stopped");
}
token = strtok(NULL, " \t");
}
if (argc == 1 || (strcmp(status, "Running") == 0 && strcmp(argv[1], "-r") == 0) || (strcmp(status, "Stopped") == 0 && strcmp(argv[1], "-s") == 0))
printf("[%d] %s %s [%d] \n", job, status, command, pid);
temp = temp->next;
}
}