-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobs.c
148 lines (135 loc) · 3.43 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
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "jobs.h"
char* _status(struct Job* job) {
char* statusString = (char*) malloc(sizeof(char) * 8);
switch(job->status) {
case JOB_RUNNING:
strcpy(statusString, "Running");
break;
case JOB_STOPPED:
strcpy(statusString, "Stopped");
break;
case JOB_DONE:
strcpy(statusString, "Done");
break;
}
return statusString;
}
void fg_job() {
if (jobs->numJobs < 1) {
return;
}
struct Job* lastJob = jobs->jobs[jobs->lastJob];
printf("%s\n", lastJob->command);
kill(-lastJob->pgid, SIGCONT);
gpidRunning = lastJob->pgid;
int status;
waitpid(-lastJob->pgid, &status, WUNTRACED);
tcsetpgrp(STDIN_FILENO, getpgrp());
if (WIFSTOPPED(status)) {
printf("\n");
}
}
void bg_job() {
if (jobs->numJobs < 1) {
return;
}
struct Job* lastJob = jobs->jobs[jobs->lastJob];
printf("%s &\n", lastJob->command);
kill(-lastJob->pgid, SIGCONT);
set_job_status(jobs->lastJob, JOB_RUNNING);
}
void init_jobs(int maxJobs) {
jobs->jobs = (struct Job**) malloc(sizeof(struct Job*) * maxJobs);
for (int i = 0; i < maxJobs; i++) {
jobs->jobs[i] = NULL;
}
jobs->maxJobs = maxJobs;
jobs->numJobs = 0;
jobs->highestJob = -1;
jobs->lastJob = -1;
}
// Returns job number
int add_job(int pgid, char* input) {
int addIndex = jobs->highestJob + 1;
jobs->jobs[addIndex] = (struct Job*) malloc(sizeof(struct Job));
struct Job* addedJob = jobs->jobs[addIndex];
addedJob->pgid = pgid;
addedJob->status = JOB_RUNNING;
addedJob->command = input;
jobs->numJobs++;
jobs->highestJob++;
jobs->lastJob = addIndex;
return addIndex;
}
void remove_job(int jobNumber) {
// Free the job being removed
free(jobs->jobs[jobNumber]->command);
free(jobs->jobs[jobNumber]);
jobs->jobs[jobNumber] = NULL;
for (int i = jobs->highestJob; i >= 0; i--) {
if (jobs->jobs[i] != NULL) {
jobs->lastJob = i;
jobs->highestJob = i;
break;
}
}
// Decrement number of jobs
jobs->numJobs--;
}
void update_jobs() {
for (int i = 0; i < jobs->maxJobs; i++) {
if (jobs->jobs[i] != NULL) {
int pgid = jobs->jobs[i]->pgid;
int status;
if (waitpid(-pgid, &status, WNOHANG | WUNTRACED) == pgid) {
if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGINT) {
remove_job(jobs->lastJob);
}
}
if (WIFEXITED(status)) {
jobs->jobs[i]->status = JOB_DONE;
jobDone = true;
}
}
}
}
}
void print_jobs() {
for (int i = 0; i < jobs->maxJobs; i++) {
struct Job* job = jobs->jobs[i];
if (job != NULL) {
char* statusString = _status(job);
char bg[3] = " &";
if (strcmp(statusString, "Stopped") == 0 || strchr(job->command, '&') != NULL) {
bg[0] = 0;
}
printf("[%d]%c\t%s\t\t%s%s\n", i + 1, i == jobs->lastJob ? '+' : '-', statusString, job->command, bg);
if (job->status == JOB_DONE) {
remove_job(i);
}
free(statusString);
}
}
jobDone = false;
}
void set_job_status(int jobNumber, int status) {
// Find index of element to set status
jobs->jobs[jobNumber]->status = status;
}
void free_jobs() {
for (int i = 0; i < jobs->maxJobs; i++) {
if (jobs->jobs[i] != NULL) {
free(jobs->jobs[i]->command);
free(jobs->jobs[i]);
}
}
free(jobs->jobs);
}