-
Notifications
You must be signed in to change notification settings - Fork 2
/
dshell.c
296 lines (270 loc) · 7.89 KB
/
dshell.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <readline/readline.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
char **get_input(char *);
int cd(char *);
void mimicshelldisplay();
void childProcess(char **);
void printcommand(char **);
int main()
{
char **command;
char *input;
pid_t child_pid, child1_pid, child2_pid;
int stat_loc;
/** Used in order to ignore both Ctrl+C(SIGINT) and
Ctrl+Z(SIGKILL) in the shell*/
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
while (1)
{
mimicshelldisplay();
input = readline("$");
command = get_input(input);
if (!command[0])
{
/* Handle empty commands */
free(input);
free(command);
continue;
}
//Implements the exit function, type "exit" to execute
if (strcmp(command[0], "exit") == 0) {
printf("Goodbye!\n");
return 0;
}
//Flags which seperate between double and triple ampersands
int iterator = 0,dflag = 0,tflag = 0;
while(command[iterator])
{
if(strcmp(command[iterator],"&&") == 0)
{
dflag = 1;
break;
}
if(strcmp(command[iterator],"&&&") == 0)
{
tflag = 1;
break;
}
iterator++;
}
if(dflag || tflag)
{
//Seperating command to two seperate commands in case of ampersand usage
char string1[1000] = "";
char string2[1000] = "";
int iter = 0;
while(iter < iterator)
{
strcat(string1,command[iter]);
strcat(string1," ");
iter++;
}
iter++;
while(command[iter])
{
strcat(string2,command[iter]);
strcat(string2," ");
iter++;
}
char **command1;
char **command2;
command1 = get_input(string1);
command2 = get_input(string2);
//Considering parallel execution via "&&&"
if(tflag){
child1_pid = fork();
child2_pid = fork();
if (child1_pid < 0 || child2_pid < 0) {
printf("Fork failed");
exit(1);
}
//Parent node
if (child1_pid > 0 && child2_pid > 0) {
waitpid(child1_pid, &stat_loc, WUNTRACED);
waitpid(child2_pid, &stat_loc, WUNTRACED);
}
//First child
if (child1_pid == 0 && child2_pid > 0) {
childProcess(command1);
}
//Second child
if (child1_pid > 0 && child2_pid == 0) {
childProcess(command2);
}
//Third child, a byproduct of forking
else if(child1_pid == 0 && child2_pid == 0) {
exit(0);
}
}
if(dflag){
int a;
for(a = 0; a < 2; a++)
{
child_pid = fork();
if (child_pid < 0) {
printf("Fork failed");
exit(1);
}
//First child
if (child_pid == 0) {
if(a == 0){
childProcess(command1);
}
if(a == 1)
{
childProcess(command2);
}
}
//Parent node
else {
waitpid(child_pid, &stat_loc, WUNTRACED);
}
}
}
}
else
{
child_pid = fork();
if (child_pid < 0) {
printf("Fork failed");
exit(1);
}
//First child
if (child_pid == 0) {
childProcess(command);
}
//Parent node
else {
waitpid(child_pid, &stat_loc, WUNTRACED);
}
}
if (!input)
free(input);
if (!command)
free(command);
}
return 0;
}
// Returns an array of strings seperated by ' '
char **get_input(char *input) {
char **command = malloc(8 * sizeof(char *));
char *separator = " ";
char *parsed;
int index = 0;
// Splits input according to given delimiters.
parsed = strtok(input, separator);
while (parsed != NULL) {
command[index] = parsed;
index++;
parsed = strtok(NULL, separator);
}
command[index] = NULL;
return command;
}
//Function to execute cd command
int cd(char *path) {
return chdir(path);
}
//Function that mimics the way regular shell prompt data is displayed
void mimicshelldisplay()
{
char cwd[PATH_MAX];
char hostname[HOST_NAME_MAX];
char username[LOGIN_NAME_MAX];
//Used to get current directory
getcwd(cwd, sizeof(cwd));
//Used for getting login and hostnames, source is unistd.h
gethostname(hostname, HOST_NAME_MAX);
getlogin_r(username, LOGIN_NAME_MAX);
//Mimics output from original shell
printf("%s@%s:%s",username,hostname,cwd);
}
//Used to execute child process
void childProcess(char **command)
{
/*Reverts signals back to original behaviors, so that processes
can be killed during execution in children*/
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
//Code for implementing alterations to STDIN and STDOUT
int i = 0;
while(command[i])
{
/*NOTE : alterations for STDOUT are placed here before STDIN
as the former has a lower fd index, hence conflicts in allocation
will not occur. (Else corruption and unexpected behaviour may happen)*/
int altered = 0;
if(strcmp(command[i],">") == 0) {
altered = 1;
close(STDOUT_FILENO);
open(command[i+1], O_CREAT | O_WRONLY | O_APPEND , S_IRWXU);
}
if(strcmp(command[i],"<") == 0) {
altered = 1;
close(STDIN_FILENO);
open(command[i+1], O_RDONLY);
}
if(altered)
{
/* Used in order to remove overhead arguments from the
i/o site when stream changing is completed*/
int j = i;
while(command[j+2])
{
strcpy(command[j],command[j+2]);
j++;
}
command[j] = NULL;
command[j+1] = NULL;
}
i++;
}
/** Used in order to detect and execute cd, as it is not a shell
program but a system call */
if (strcmp(command[0], "cd") == 0) {
//Error handling
if (cd(command[1]) < 0) {
printf("Error in changing directory. Kindly enter a valid input.");
}
/* Skips the fork, as child process is no longer required */
return;
}
//Implements the exit function, type "exit" to execute
if (strcmp(command[0], "exit") == 0) {
printf("Goodbye!\n");
return;
}
if (strcmp(command[0], "clear") == 0) {
printf("\033[H\033[J");
/* Skips the fork, as child process is no longer required */
return;
}
/* Never returns if the call is successful */
int cmdflag = execvp(command[0],command);
if (cmdflag < 0) {
printf("Shell : Incorrect command\n");
exit(1);
}
}
//Used for testing purposes
void printcommand(char **command)
{
int z = 0;
while(command[z])
{
printf("%s ",command[z]);
z++;
}
printf("%d : length of string",z);
printf("\n");
}