-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
71 lines (68 loc) · 2.25 KB
/
execute.c
File metadata and controls
71 lines (68 loc) · 2.25 KB
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
#include "headers.h"
#include "pinfo.h"
#include "globals.h"
#include "definitions.h"
#include "childproc.h"
void execute(char **data, int len, char *trimmedCommand, int isBackground)
{
if (data != NULL && len > 0)
{
int pid = fork();
if (pid > 0)
{
//parent
setCurrentFgPid(pid);
if (isBackground != 1)
{
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
tcsetpgrp(STDIN_FILENO, pid);
// if (tcsetpgrp(STDIN_FILENO, pid) == -1)
// {
// perror("Error, process cannot be made foreground");
// return;
// }
int status;
if (waitpid(pid, &status, WUNTRACED) == -1)
{
perror("Error, process cannot finish");
// terminalFG();
return;
}
if (WIFSTOPPED(status) != 0)
{
if (getCurrentFgPid() != -1)
{
processDetails *node = addProcNode(pid, data[0], trimmedCommand, getChildProcessList());
printf("[%d] %d\n", node->jobid, node->pid);
}
}
tcsetpgrp(STDIN_FILENO, getpgrp());
// if (tcsetpgrp(STDIN_FILENO, getpgrp()) == -1)
// {
// perror("Error, terminal cannot be made foreground");
// return;
// }
signal(SIGTTIN, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
setCurrentFgPid(-1);
}
else
{
processDetails *node = addProcNode(pid, data[0], trimmedCommand, getChildProcessList());
printf("[%d] %d\n", node->jobid, node->pid);
}
}
else if (pid == 0)
{
setpgid(0, 0);
execvp(data[0], data);
printf("%s: Command not found\n", data[0]);
exit(0);
}
else
{
printf("Error: Process can not be created\n");
}
}
}