-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.c
70 lines (51 loc) · 1.24 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
#include "jobs.h"
#include <stdio.h>
#include <string.h>
#include <wait.h>
#include <unistd.h>
struct cmd_simple jobs_array[MAX_JOBS];
// Array index of last added job
int job_counter = -1;
// Returns -1 on fail, otherwise returns array index
int add_job(struct cmd_simple cmd) {
if(job_counter >= MAX_JOBS - 1) {
return -1;
}
job_counter++;
jobs_array[job_counter] = cmd;
return job_counter;
}
void remove_job_by_cmd(struct cmd_simple *cmd) {
for(int i = 0; i <= job_counter; i++) {
if(memcmp(&jobs_array[i], cmd, sizeof(struct cmd_simple)) == 0) {
remove_job_by_index(i);
break;
}
}
}
void remove_job_by_index(int index) {
for(int i = index; i < MAX_JOBS - 1; i++) {
jobs_array[i] = jobs_array[i + 1];
}
job_counter--;
}
void print_jobs() {
for(int i = 0; i <= job_counter; i++) {
char *program_name = cmd_extract_program(&jobs_array[i]);
printf("%d %s\n", i+1, program_name);
}
}
void foreground_job(int index) {
if(index < 0 || index > job_counter) {
printf("Invalid job number");
}
struct cmd_simple cmd = jobs_array[index];
// Overwrite our stdin with the process' stdin
int old_stdin = dup(0);
close(0);
dup(cmd.write_fd);
waitpid(cmd.pid, 0, 0);
// Undo last comment
close(0);
dup(old_stdin);
}