-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
224 lines (211 loc) · 4.79 KB
/
shell.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Bash made by Vansh Marda
// 2021101089
// IIIT H - CSE
#include "headers.h"
char *remove_whitespace(char *line)
{
// Calculating length of the array
// len = sizeof(str)/sizeof(str[0]);
// Checks for space character in array if its there then ignores it and swap str[i] to str[i+1];
int t = 0;
int i = 0;
// for(i = 0; i < len; i++){
// if(str[i] == ' '){
// for(j=i;j<len;j++)
// {
// str[j]=str[j+1];
// }
// len--;
// }
// }
while (i < strlen(line))
{
if (line[i] == '\n')
{
t++;
}
else if(line[i]=='\t'){
t++;
}
else if(line[i]==' '){
t++;
}
else
{
break;
}
i++;
}
i = 0;
while (i < t)
{
line++;
i++;
}
i = strlen(line) - 1;
while (i >= 0)
{
if (line[i] == '\n')
{
line[i]='\0';
}
else if(line[i]=='\t'){
line[i]='\0';
}
else if(line[i]==' '){
line[i]='\0';
}
else
{
break;
}
i--;
}
// printf(%s", line);
return line;
}
// separates commands by ;
void input_commands(char *arr)
{
char *command;
char arr2[buffer];
char arr3[buffer];
strcpy(arr2, arr);
strcpy(arr3, arr);
int c = 0;
command = strtok(arr, ";&");
int x = strlen(command);
int z = 0;
for (int i = 0; i < x; i++)
{
z++;
}
while (command != NULL)
{
c++;
command = strtok(NULL, ";&");
}
if (c <= 0)
{
return;
}
char *input_commands[c + 1];
int i = 0;
char *beg = arr2;
input_commands[0] = strtok(arr2, ";&");
int c_pid = -1;
if (c_pid == 0)
{
struct sigaction sigterm_action;
memset(&sigterm_action, 0, sizeof(sigterm_action));
sigterm_action.sa_handler = 0;
sigterm_action.sa_flags = 0;
// Mask other signals from interrupting SIGTERM handler
if (sigfillset(&sigterm_action.sa_mask) != 0)
{
perror("sigfillset");
exit(EXIT_FAILURE);
}
// Register SIGTERM handler
if (sigaction(SIGTERM, &sigterm_action, NULL) != 0)
{
perror("sigaction SIGTERM");
exit(EXIT_FAILURE);
}
}
while (input_commands[i] != NULL && strcmp(input_commands[i], "") != 0)
{
i++;
input_commands[i] = strtok(NULL, ";&");
}
int j = 0;
while (j < c)
{
bool bg = false;
if (arr3[strlen(input_commands[j]) + (input_commands[j] - beg)] == '&')
{
bg = true;
}
input_commands[j] = remove_whitespace(input_commands[j]);
int backup_stdout = dup(STDOUT_FILENO);
int backup_stdin = dup(STDIN_FILENO);
pipeChecker(input_commands[j], bg);
dup2(backup_stdout, 1);
close(backup_stdout);
dup2(backup_stdin, 0);
close(backup_stdin);
j++;
}
}
void signalHandler(int sig_num) // handles the signals like Ctrl+C , Ctrl+D
{
if (sig_num == SIGINT)
{
}
return;
}
void rip_child(int signum)
{
if (signum == SIGCHLD)
background_process_killer();
}
void exit_2(int signum)
{
if (signum == SIGINT)
{
kill_background();
_exit(0);
}
}
int main()
{
printf("\e[1;1H\e[2J");
printf(" ---------------\n");
printf("| Welcome |\n");
printf("| to |\n");
printf("| Vansh Marda's |\n");
printf("| Shell ;) |\n");
printf(" ---------------\n");
// vansh@vansh-X550CL:
char *shellName;
shellName = username();
// Get the pathname of the current working directory
// home dir ---> /home/vansh/Desktop/osn/assignment/assignment2/
if (getcwd(initialDirector, buffer) == NULL)
{
perror("Cannot get path to home dir failed");
}
strcat(initialDirector, "/");
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
strcpy(workingDirectory, initialDirector);
TransformDir();
while (1)
{
printf("%s", "\033[1m\033[33m");
printf("%s", "\033[1m\033[0m");
printInitials(shellName, showingDirectory);
char *arr = malloc(buffer);
if (fgets(arr, buffer, stdin) == NULL)
{
kill_background();
printf("cya\n");
_exit(0);
}
int ln = strlen(arr) - 1;
if (*arr)
{
if (arr[ln] == '\n')
{
arr[ln] = '\0';
}
}
arr = remove_whitespace(arr);
if (strcmp(arr, "") != 0)
{
add_history(arr);
input_commands(arr);
}
}
}