-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
108 lines (104 loc) · 3.2 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
#include "display.h"
#include "log.h"
#include<stdio.h>
#include<string.h>
#include "background.h"
#include "foreground.h"
#include<stdlib.h>
#include<unistd.h>
#include "log.h"
#include <sys/wait.h>
#include "IO.h"
#include "pipe.h"
#include "sig.h"
#include "process.h"
// #include<signal.h>
// #include "activities.h"
int shell_pid;
char* home_dir;
int main() {
char home[4096];
getcwd(home, sizeof(home));
initialize_process_list(4096);
home_dir = home;
printf("HOME directory is %s\n",home);
int foreground = 0;
char foreground_Command[4096];
int pgid = getpgid(getpid());
// printf("pgid: %d\n",pgid);
int pid = getpid();
printf("PID of the SHELL: %d\n",pid);
shell_pid = pid;
while(1){
signal(SIGCHLD,handle_background_termination);
signal(SIGINT,handle_sigint);
signal(SIGTSTP,handle_sigstp);
PrintUser(home,foreground,foreground_Command);
foreground=0;
char command[4096];
if(fgets(command, sizeof(command), stdin) == NULL) {
if (feof(stdin)) {
// End-of-file (Ctrl-D)
handle_sigquit();
} else if (ferror(stdin)) {
// Error reading input
perror("Error reading input");
clearerr(stdin); // Clear the error
continue;
}
else{
continue;
}
}
log_command(command,home);
char* token = strtok(command, ";");
while(token != NULL) {
token[strcspn(token, "\n")] = '\0';
int n= strlen(token);
int background = 0;
int io_redirect=0;
int pipe=0;
for(int i=0;i<n;i++){
if(token[i]=='&'){
background = 1;
break;
}
else if(token[i]=='>' || token[i]=='<'){
io_redirect=1;
}
else if(token[i]=='|'){
pipe = pipe +1;
}
}
if(background){
make_agrs(token);
// printf("returning from background\n");
}
else if(io_redirect && !pipe){
redirect(token,home,pid);
}
else if(pipe){
pipe_handling(pipe,token,home,pid);
}
else{
foreground = foreground_execute(token,home,pid);
int i=0;
// printf("received; %d\n",foreground);
if(foreground>0){
while(token[i]==' '){
i++;
}
while(token[i]!='\0' && token[i]!=' '){
foreground_Command[i]=token[i];
i++;
}
foreground_Command[i]='\0';
// printf("%s\n",foreground_Command);
}
}
token = strtok(NULL, ";");
}
}
free(processes);
return 0;
}