-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.c
178 lines (164 loc) · 3.7 KB
/
list.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include "list.h"
#include "jobs.h"
/*count running jobs*/
int count(Running_List *jobs_list) {
int count = 0;
Running_List templist;
templist = *jobs_list;
if (templist == NULL)
return 0;
while (templist != NULL) {
count++;
templist = templist->next;
}
return count;
}
/*count queued jobs*/
int count_queued(Queued_List *list) {
int count = 0;
Queued_List templist;
templist = *list;
if (templist == NULL)
return 0;
while (templist != NULL) {
count++;
templist = templist->next;
}
return count;
}
/*add job to running jobs list*/
void addToRunning(Running_List *running_jobs_list, int jobID, char *job, int pid) {
Running_List templist;
templist = *running_jobs_list;
/*first running job*/
if (templist == NULL) {
*running_jobs_list = malloc(sizeof(struct r_listnode));
(*running_jobs_list)->jobID = jobID;
(*running_jobs_list)->pid = pid;
strcpy((*running_jobs_list)->job, job);
(*running_jobs_list)->next = templist;
}
/*not the first job in the list*/
else {
while (*running_jobs_list != NULL)
running_jobs_list = &((*running_jobs_list)->next);
*running_jobs_list = malloc(sizeof(struct r_listnode));
(*running_jobs_list)->jobID = jobID;
(*running_jobs_list)->pid = pid;
strcpy((*running_jobs_list)->job, job);
(*running_jobs_list)->next = NULL;
}
}
/*add job to queued jobs list*/
void addToQueued(Queued_List *list, int jobID, char *job) {
Queued_List templist;
templist = *list;
/*first running job*/
if (templist == NULL) {
*list = malloc(sizeof(struct q_listnode));
(*list)->jobID = jobID;
strcpy((*list)->job, job);
(*list)->next = templist;
}
/*not the first job in the list*/
else {
while (*list != NULL)
list = &((*list)->next);
*list = malloc(sizeof(struct q_listnode));
(*list)->jobID = jobID;
strcpy((*list)->job, job);
(*list)->next = NULL;
}
}
/*print running jobs*/
void print_running(Running_List list, char **args) {
int i = 0;
char buffer[500];
while (list != NULL) {
sprintf(buffer, "%d\t%s", list->jobID, list->job);
args[i] = malloc(sizeof(char)*(strlen(buffer)+1));
strcpy(args[i], buffer);
i++;
list = list->next;
}
args[i] = NULL;
}
/*print queued jobs*/
void print_queued(Queued_List list, char **args) {
int i;
char buffer[500];
i = 0;
while (list != NULL) {
sprintf(buffer, "%d\t%s", list->jobID, list->job);
args[i] = malloc(sizeof(char)*(strlen(buffer)+1));
strcpy(args[i], buffer);
i++;
list = list->next;
}
args[i] = NULL;
}
/*stop running job*/
int stop(Running_List *list, int jobID) {
Running_List templist;
while ((*list) != NULL) {
if ((*list)->jobID == jobID) {
templist = *list;
kill((*list)->pid, SIGKILL);
*list = (*list)->next;
free(templist);
return 1;
}
else
list = &((*list)->next);
}
return 0;
}
/*stop queued job*/
int stop_queued(Queued_List *list, int jobID) {
Queued_List templist;
while ((*list) != NULL) {
if ((*list)->jobID == jobID) {
templist = *list;
*list = (*list)->next;
free(templist);
return 1;
}
else
list = &((*list)->next);
}
return 0;
}
/*remove running job from list*/
int remove_running(int pid, Running_List *list) {
Running_List templist;
while (*list != NULL) {
if ((*list)->pid == pid) {
templist = *list;
*list = (*list)->next;
free(templist);
return 1;
}
else
list = &((*list)->next);
}
return 0;
}
/*remove queued job from list*/
int remove_queued() {
Queued_List templist;
if ((queued_jobs_list) != NULL) {
templist = queued_jobs_list;
queued_jobs_list = (queued_jobs_list)->next;
free(templist);
return 1;
}
return 0;
}