-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
158 lines (122 loc) · 3.7 KB
/
main.c
File metadata and controls
158 lines (122 loc) · 3.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "headers.h"
#include "global_vars.h"
#include "all_fns.h"
void printPrompt(char * user_name, char * system_name, char * currDir, char * homeDir,char prompt_path[SIZ], int cmdT_gt2_flag){
if (dup2(saved_stdin, STDIN_FILENO) == -1) {
perror("dup2");
return;
}
if (dup2(saved_stdout, STDOUT_FILENO) == -1) {
perror("dup2");
return;
}
char original_prompt_path[SIZ];
if(cmdT_gt2_flag==1){
strcpy(original_prompt_path, prompt_path);
strcat(prompt_path, cmd_gt2_add);
cmdT_gt2_flag = 0;
strcpy(cmd_gt2_add, "");
}
printf(PINK_COLOR "<" DARK_SEA_GREEN_COLOR "%s" ORG_COLOR "@" LIGHT_CYAN_COLOR "%s" ORG_COLOR ":" YELLOW_COLOR "%s" PINK_COLOR "> " ORG_COLOR ,user_name,system_name,prompt_path);
if (cmdT_gt2_flag==0) {
strcpy(prompt_path, original_prompt_path);
handling_prompt();
}
}
void handle_sigchld(int sig) {
int saved_errno = errno;
pid_t pid;
int status;
while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
if (WIFEXITED(status)) {
remove_process(pid);
printf("Process with PID %d exited with status %d\n", pid, WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
remove_process(pid);
printf("Process with PID %d was killed by signal %d\n", pid, WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
update_process_state(pid, 0);
printf("Process with PID %d was stopped by signal %d\n", pid, WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
update_process_state(pid, 1);
printf("Process with PID %d continued\n", pid);
}
}
errno = saved_errno;
}
int main(){
char pwd[SIZ];
if(getcwd(pwd,sizeof(pwd))==NULL){
perror("getcwd");
return 1;
}
strcpy(homeDir,pwd);
strcpy(currDir,pwd);
uid_t user_id = getuid();
struct passwd * det = getpwuid(user_id);
char user_name[SIZ];
if(det!=NULL){
strcpy(user_name,det->pw_name);
}
else{
perror("getpwuid");
return 1;
}
char system_name[SIZ];
if(gethostname(system_name,sizeof(system_name))!=0){
perror("gethostname");
return 1;
}
strcpy(prompt_path,"~");
PID_SHELL=getpid();
saved_stdin = dup(STDIN_FILENO);
if (saved_stdin == -1) {
perror("dup");
return 1;
}
saved_stdout=dup(STDOUT_FILENO);
if(saved_stdout==-1){
perror("dup");
return 1;
}
signal(SIGINT, handle_sigint_ctrlC);
signal(SIGTSTP, handle_sigtstp_ctrlZ);
// load_myshrc();
int fd = open("log_file.txt", O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
load_existing_commands();
struct sigaction sa;
sa.sa_handler = &handle_sigchld;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART ;
// | SA_NOCLDSTOP
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
while(1){
// handle_BGP();
printPrompt(user_name,system_name,currDir,homeDir,prompt_path,cmdT_gt2_flag);
char inp[SIZ];
if (fgets(inp, sizeof(inp), stdin) != NULL) {
inp[strcspn(inp, "\n")] = '\0';
if (strlen(inp) == 0) {
continue;
}
char store_1[SIZ];
strcpy(store_1,inp);
tokenize(inp);
log_help1(store_1);
}
else {
if (feof(stdin)) {
kill_all_processes();
printf("\nExiting shell...\n");
break;
}
else {
perror("fgets");
}
}
}
return 0;
}