-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystemcommand.c
82 lines (77 loc) · 1.85 KB
/
systemcommand.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
#include "headers.h"
#include "prompt.h"
#include "systemcommand.h"
#include "main.h"
#include "pinfo.h"
#include "history.h"
#include "jobs.h"
#include "signalhandler.h"
int ongoing_process = 0;
void systemcommand(char **argument, int no_of_arg)
{
int background = 0;
if (strcmp(argument[no_of_arg - 1], "&") == 0)
{
background = 1;
}
int pid = fork();
if (pid < 0)
{
perror("fork: ");
}
else if (pid == 0)
{
if (background == 1)
{
setpgid(0, 0);
close(STDERR_FILENO);
//close(STDOUT_FILENO);
// close(STDIN_FILENO);
argument[no_of_arg - 1] = 0;
}
//ctrlz and ctrlc
// signal(SIGINT, SIG_DFL);
// signal(SIGTSTP, SIG_DFL);
// printf("1\n");
// int t = execvp(argument[0], argument);
// printf("11\n");
if (strcmp(argument[0], "history") == 0)
{
history(no_of_arg - 1, argument);
}
else if (strcmp(argument[0], "pinfo") == 0)
{
pinfo(argument, no_of_arg - 1);
}
else if (strcmp(argument[0], "jobs") == 0)
{
jobs(argument, no_of_arg - 1);
}
else
{
if (execvp(argument[0], argument) == -1)
{
perror("Execute : ");
}
}
exit(1);
}
else
{
if (background == 0)
{
ongoing_process = pid;
int status;
waitpid(pid, &status, WUNTRACED);
ongoing_process = 0;
}
else
{
printf("%d\n", pid);
//signal(SIGCHLD, SIG_IGN);
backgroundpid[backgroundprocess] = pid;
strcpy(background_process[backgroundprocess], argument[0]);
backgroundprocess++;
}
}
}