-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobs.c
118 lines (99 loc) · 2.54 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
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
#include "Headers.h"
#include "parse.h"
#include "echo.h"
#include "ls.h"
#include "cd.h"
#include "pwd.h"
#include "Username.h"
#include "curdir.h"
#include "pinfo.h"
#include "history.h"
#include "jobs.h"
int comparator(const void *p, const void *q)
{
background_jobs *element1 = (background_jobs *)p;
background_jobs *element2 = (background_jobs *)q;
if(element1->name[0] < element2->name[0])
{
return -1;
}
if(element1->name[0] > element2->name[0])
{
return 1;
}
return 0;
}
void print_jobs(background_jobs *background_job, char *flags)
{
background_jobs copy[20];
for(int i=0; i<20; i++)
{
strcpy(copy[i].name, background_job[i].name);
copy[i].id = background_job[i].id;
copy[i].number = background_job[i].number;
}
int running=0;
int stopped=0;
if(flags==NULL)
{
running=1;
stopped=1;
}
else
{
char *check_flags = strtok(flags," \t");
while(check_flags!=NULL)
{
if(strcmp(check_flags, "-r")==0)
{
running=1;
}
if(strcmp(check_flags,"-s")==0)
{
stopped=1;
}
if(strcmp(check_flags,"-sr")==0 || strcmp(check_flags,"-rs")==0)
{
running=1;
stopped=1;
}
check_flags=strtok(NULL," \t");
}
}
char line[100];
char *status;
FILE *fd;
qsort(copy,20, sizeof(copy[0]),comparator);
char *path;
for (int i = 0; i < 20; i++)
{
if(copy[i].id!=0)
{
path = (char*)malloc(50*sizeof(char));
snprintf(path, 50, "/proc/%d/stat", copy[i].id);
fd = fopen(path, "r");
for (int i = 0; i < 23; i++)
{
if (i == 2)
{
fscanf(fd, "%s", line);
status = (char *)malloc(10);
strcpy(status, line);
break;
}
else
{
fscanf(fd, "%s", line);
}
}
if(strcmp(status,"T")==0 && stopped==1)
{
printf("[%d] %s %s [%d]\n",copy[i].number, "Stopped", copy[i].name, copy[i].id);
}
if(strcmp(status,"T")!=0 && running==1)
{
printf("[%d] %s %s [%d]\n",copy[i].number, "Running", copy[i].name, copy[i].id);
}
}
}
}