-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
101 lines (85 loc) · 3.11 KB
/
Copy pathmain.c
File metadata and controls
101 lines (85 loc) · 3.11 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
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
#include "headers.h"
#include "tools.h"
#include "handleInput.h"
char *global_home = NULL;
char *prev_directory = NULL;
struct ProcessNode *process_list_head = NULL;
void handleCtrlC(int signal) {
if (process_list_head != NULL) {
pid_t foregroundPid = process_list_head->process_info.pid;
if (kill(foregroundPid, SIGINT) == 0) {
printError("Terminated process with PID %d\n", foregroundPid);
updateProcessState(foregroundPid, "Terminated");
}
} else {
printf("No foreground process running to terminate.\n");
}
}
void handleCtrlZ(int signal) {
if (process_list_head != NULL) {
pid_t foregroundPid = process_list_head->process_info.pid;
if (foregroundPid == getpid()) {
// The foreground process is the shell itself, so Ctrl+Z has no effect
printError("Ctrl+Z: No foreground process running to stop.\n");
} else {
// Send the SIGTSTP signal to the foreground process
if (kill(foregroundPid, SIGTSTP) == 0) {
// Print a message indicating Ctrl+Z was pressed
printError("Ctrl+Z: Pushed running process with PID %d to background\n", foregroundPid);
// Update the state of the foreground process to "Stopped"
updateProcessState(foregroundPid, "Stopped");
// Run the foreground process in the background
if (setpgid(foregroundPid, foregroundPid) == 0) {
// Successfully moved to the background
} else {
printError("Ctrl+Z: Error moving the process to the background");
}
} else {
printError("Ctrl+Z: Error sending SIGTSTP signal");
}
}
} else {
// No foreground process running, so Ctrl+Z has no effect
printError("Ctrl+Z: No foreground process running to stop.\n");
}
}
int main() {
// struct ProcessNode* process_list_head = NULL;
global_home = initializeGlobalHome();
signal(SIGINT, handleCtrlC);
signal(SIGTSTP, handleCtrlZ);
// signal(SIGQUIT, handleCtrlD);
loadPastEvents();
printWyshArt();
int backgroundProcessStatus = 0;
while (1) {
prompt(backgroundProcessStatus);
char input[MAX_INPUT_LENGTH];
// fgets(input, MAX_INPUT_LENGTH, stdin);
if (fgets(input, MAX_INPUT_LENGTH, stdin) == NULL) {
// Handle Ctrl+D (EOF)
printf("Ctrl+D received, logging out...\n");
break; // Exit the shell
}
input[strcspn(input, "\n")] = '\0';
addToPastEvents(input);
printf(RESET_COLOR);
if (strcmp(input, "exit") == 0) {
for (int i = 0; i < num_past_events; i++) {
free(past_events[i]);
}
return 0;
} else {
handleInput(input);
backgroundProcessStatus = 0;
}
// viewProcesses();
}
freeProcessList();
free(global_home);
free(prev_directory);
for (int i = 0; i < num_past_events; i++) {
free(past_events[i]);
}
return 0;
}