-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
121 lines (112 loc) · 3.22 KB
/
main.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
#include "headers.h"
#include "prompt.h"
#include "trim.h"
#include "function_decider.h"
char *currentworkingdir;
char *prevworkingdir;
char *homeworkingdir;
char *com;
int check;
int tim;
int f_check, d_check;
char *to_change;
char *s;
char *command_pid[100000 + 1];
int past = 0;
void custom_sigint_handler(int signum)
{
int targetPID = foregroundProcessPid;
if ((foregroundProcessPid == -1))
return;
if (kill(targetPID, 9) == -1)
{
perror("kill");
}
foregroundProcessPid = -1;
}
int foregroundProcessPid = -1;
void sigtstp_handler(int signum)
{
int targetPID = foregroundProcessPid;
if ((foregroundProcessPid == -1))
return;
if (kill(targetPID, SIGSTOP) == -1)
{
perror("kill");
}
foregroundProcessPid = -1;
}
struct ProcessInfo *processList = NULL;
int main()
{
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
if (signal(SIGINT, custom_sigint_handler) == SIG_ERR)
{
perror("signal");
}
if (signal(SIGTSTP, sigtstp_handler) == SIG_ERR)
{
perror("signal");
}
printf("YES");
for (int i = 0; i < 100000 + 1; i++)
{
command_pid[i] = (char *)malloc(sizeof(char) * 20);
}
s = (char *)malloc(sizeof(char) * 100);
clear();
currentworkingdir = (char *)malloc(sizeof(char) * 1024);
homeworkingdir = (char *)malloc(sizeof(char) * 1024);
prevworkingdir = (char *)malloc(sizeof(char) * 1024);
com = (char *)malloc(sizeof(char) * 1024);
to_change = (char *)malloc(sizeof(char) * 1024);
getcwd(currentworkingdir, 1024);
strcpy(prevworkingdir, currentworkingdir);
strcpy(homeworkingdir, currentworkingdir);
char *input = (char *)malloc(sizeof(char) * 1024);
while (1)
{
prompt(homeworkingdir);
past=0;
if (fgets(input, 1024, stdin) == NULL)
{
while (processList != NULL)
{
struct ProcessInfo *x = processList;
processList = processList->next;
if (kill(x->pid, 9) == 0)
{
printf("Signal %d sent to process with PID %d\n", 9, x->pid);
}
else
{
perror("kill");
return 1;
}
processList = removeProcess(processList, x->pid);
}
printf("\n");
exit(0);
}
Node *currentNode = completedList;
while (currentNode != NULL)
{
if (WIFEXITED(currentNode->status))
{
printf("%s process with PID %d exited normally with status %d\n", command_pid[currentNode->pid], currentNode->pid, WEXITSTATUS(currentNode->status));
}
else if (WIFSIGNALED(currentNode->status))
{
printf("%s process with PID %d exited abnormally due to signal %d\n", command_pid[currentNode->pid], currentNode->pid, WTERMSIG(currentNode->status));
}
Node *temp = currentNode;
currentNode = currentNode->next;
free(temp);
}
completedList = NULL;
input = trimspace(input);
function_call(input);
}
}