-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.c
161 lines (155 loc) · 4.68 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "headers.h"
int compare(const void *p1, const void *p2)
{
const struct checkJobs *elem1 = p1;
const struct checkJobs *elem2 = p2;
if (strcmp(elem1->jobname, elem2->jobname) == 0)
{
return 0;
}
else
{
int x = strlen(elem1->jobname), retval = -1;
if (strlen(elem2->jobname) < x)
{
x = strlen(elem2->jobname);
retval = 1;
}
for (int i = 0; i < x; i++)
{
if (elem1->jobname[i] > elem2->jobname[i])
{
return 1;
}
else if (elem1->jobname[i] < elem2->jobname[i])
{
return -1;
}
}
return retval;
}
}
int jobs(char *cmd)
{
char buf[MAX_SIZE * 10];
char *token1;
char *res = cmd;
strtok_r(NULL, " ", &res);
char flag[200];
flag[0] = '\0';
int r_flag = 0, s_flag = 0;
while ((token1 = strtok_r(NULL, " ", &res)) != NULL)
{
if (strcmp("-r", token1) == 0)
{
r_flag = 1;
}
if (strcmp("-s", token1) == 0)
{
s_flag = 1;
}
if ((strcmp("-sr", token1) == 0) || (strcmp("-rs", token1) == 0))
{
s_flag = 1;
r_flag = 1;
}
}
for (int i = 0; i < totalbgJobs; i++)
{
char buf[1000];
int pid = jobsArr[i].pid;
// printf("%d\n", pid);
sprintf(buf, "/proc/%d/status", pid);
// printf("%s\n", buf);
FILE *fp = fopen(buf, "r");
if (!fp)
{
jobsArr[i].status='E';
// printf("jobs: error opening status file");
continue;
}
char *line = NULL;
size_t len = 0;
int x = 0;
char finStatus = 'E';
while ((getline(&line, &len, fp)) != -1)
{
if (x == 2)
{
int id = getpgid(pid);
line[strcspn(line, "\n")] = 0;
char *token111 = strtok_r(NULL, " ", &line);
// token111=strtok_r(NULL," ",&line);
finStatus = token111[strlen(token111) - 1];
break;
}
x++;
}
// printf("%c\n",finStatus);
jobsArr[i].status = finStatus;
}
struct checkJobs useless;
qsort(jobsArr, totalbgJobs, sizeof(useless), compare);
for (int i = 0; i < totalbgJobs; i++)
{
if (jobsArr[i].status == 'T')
{
printf("[%d] Stopped %s [%d]\n", jobsArr[i].jobid, jobsArr[i].jobname, jobsArr[i].pid);
}
else if (jobsArr[i].status == 'R' || jobsArr[i].status == 'S')
{
printf("[%d] Running %s [%d]\n", jobsArr[i].jobid, jobsArr[i].jobname, jobsArr[i].pid);
}
}
return 0;
// int status, pid, r = 0, s = 0;
// struct checkJobs *running = (struct checkJobs *)malloc(sizeof(struct checkJobs) * MAX_SIZE);
// struct checkJobs *stopped = (struct checkJobs *)malloc(sizeof(struct checkJobs) * MAX_SIZE);
// for (int i = 0; i < totalbgJobs; i++)
// {
// if (jobsArr[i].status == 'R')
// {
// running[r]=jobsArr[i];
// running[r].pid = jobsArr[i].pid;
// running[r].status = jobsArr[i].status;
// strcpy(running[r++].jobname, jobsArr[i].jobname);
// }
// else if (jobsArr[i].status == 'T')
// {
// stopped[s]=jobsArr[i];
// stopped[s].pid = jobsArr[i].pid;
// stopped[s].status = jobsArr[i].status;
// strcpy(stopped[s++].jobname, jobsArr[i].jobname);
// }
// }
// struct checkJobs useless;
// qsort(running, r, sizeof(useless), compare);
// qsort(stopped, s, sizeof(useless), compare);
// printf("%d\n%c\n%d\n", totalbgJobs,jobsArr[0].status,s);
// if (r_flag == 1 && s_flag == 0)
// {
// for (int i = 0; i < r; i++)
// {
// printf("[%d] Running %s [%d]\n", running[i].jobid, running[i].jobname, running[i].pid);
// }
// }
// else if ((s_flag == 1 && r_flag == 0))
// {
// for (int i = 0; i < s; i++)
// {
// printf("[%d] Stopped %s [%d]\n", stopped[i].jobid, stopped[i].jobname, stopped[i].pid);
// }
// }
// else
// {
// for (int i = 0; i < r; i++)
// {
// printf("[%d] Running %s [%d]\n", running[i].jobid, running[i].jobname, running[i].pid);
// }
// for (int i = 0; i < s; i++)
// {
// printf("[%d] Stopped %s [%d]\n", stopped[i].jobid, stopped[i].jobname, stopped[i].pid);
// }
// }
// return 0;
}