-
Notifications
You must be signed in to change notification settings - Fork 0
/
fg.c
49 lines (38 loc) · 930 Bytes
/
fg.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
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<signal.h>
#include<termios.h>
#include"utils.h"
#include"processes.h"
void fg(Cmd_s *cmd) {
if (cmd->argc != 2) {
fprintf(stderr, "Usage: fg <job id>\n");
return;
}
int n = atoi(cmd->argv[1]);
Process_node *proc = get_by_ind(n);
if (!proc) {
fprintf(stderr, "Process %s couldn't be found\n", cmd->argv[1]);
return;
}
int status;
cmd->in_bg = 0;
proc->bg = 0;
if (kill(proc->pid, SIGCONT)) {
perror("ash: fg");
return;
}
tcsetpgrp(shell_term, proc->pid);
tcsetpgrp(STDIN_FILENO, proc->pid);
tcsetpgrp(STDOUT_FILENO, proc->pid);
waitpid(proc->pid, &status, WUNTRACED);
tcsetpgrp(shell_term, shell_pid);
tcsetpgrp(STDIN_FILENO, shell_pid);
tcsetpgrp(STDOUT_FILENO, shell_pid);
if (WIFSTOPPED(status)) {
setpgid(proc->pid, proc->pid);
proc->bg = 1;
fprintf(stderr, "\nstopped %s [%d]\n", proc->name, proc->pid);
}
}