-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
382 lines (290 loc) · 9.67 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// shell.c
// shell
//
// Created by Alex on 2016-01-26.
// Copyright © 2016 Alex. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<signal.h>
//Signal handler function dec.
void sighandler(int);
//Simple fork to execute commands without pipe or redirection
int executeCmd(char** params);
//Command length and num of parameters
#define MAX_COMMAND_LENGTH 100
#define MAX_NUMBER_OF_PARAMS 10
// Split cmd into array of parameters
int parseCmd(char* cmd, char *params[]){
char *line;
int i;
i = 0;
line = cmd;
params[i] = strtok(line, " ");
do {
i++;
line = NULL;
params[i] = strtok(line, " ");
} while (params[i] != NULL);
return i;
}
//Pipe
int do_pipe(char **args, int pipes) {
// The number of commands to run
const int commands = pipes + 1;
int i = 0;
int pipefds[2*pipes];
for(i = 0; i < pipes; i++){
if(pipe(pipefds + i*2) < 0) {
perror("Error! Couldn't Pipe");
exit(EXIT_FAILURE);
}
}
int pid;
int status;
int j = 0;
int k = 0;
int s = 1;
int place;
int commandStarts[10];
commandStarts[0] = 0;
// Set pipes to null and create an array from the next element
while (args[k] != NULL){
if(!strcmp(args[k], "|")){
args[k] = NULL;
// printf("args[%d] is now NULL", k);
commandStarts[s] = k+1;
s++;
}
k++;
}
for (i = 0; i < commands; ++i) {
// place is where in args the program should start running when it gets to the execution
place = commandStarts[i];
pid = fork();
//Child
if(pid == 0) {
if(i < pipes){
if(dup2(pipefds[j + 1], 1) < 0){
perror("dup2");
exit(EXIT_FAILURE);
}
}
if(j != 0 ){
if(dup2(pipefds[j-2], 0) < 0){
perror("dup2");
exit(EXIT_FAILURE);
}
}
int q;
for(q = 0; q < 2*pipes; q++){
close(pipefds[q]);
}
// Execute commands
if( execvp(args[place], args+place) < 0 ){
perror(*args);
exit(EXIT_FAILURE);
}
}
//Pipe Error
else if(pid < 0){
perror("Pipe Error in do_pipe function");
exit(EXIT_FAILURE);
}
j+=2;
}
for(i = 0; i < 2 * pipes; i++){
close(pipefds[i]);
}
for(i = 0; i < pipes + 1; i++){
wait(&status);
}
return 0;
}
//MAIN
int main() {
signal(SIGINT, sighandler);
char cmd[MAX_COMMAND_LENGTH + 1];
char histar[10][MAX_COMMAND_LENGTH + 1];
char hischar[MAX_COMMAND_LENGTH + 1];
int a = 0;
int piped =0;
int redirin = 0;
int redirout = 0;
int hiscount = 0;
while(1) {
char* params[MAX_NUMBER_OF_PARAMS + 1];
printf("azarifog> ");
// Read command from standard input
if(fgets(cmd, sizeof(cmd), stdin) == NULL) break;
// Remove trailing newline character, if any
if(cmd[strlen(cmd)-1] == '\n') {
cmd[strlen(cmd)-1] = '\0';
}
//Copy the input for history to save later..
strncpy(hischar, cmd, MAX_COMMAND_LENGTH + 1);
// Split cmd into array of parameters
int n = parseCmd(cmd, params);
// If exit is entered
if(strcmp(params[0], "exit") == 0) break;
//History
if(strcmp(params[0], "history") == 0){
int count_num = 1;
if(hiscount < 10) count_num = hiscount;
else count_num = 10;
for(int h = 0 ; h < count_num; h++){
printf("%s\n" , histar[h]);
}
continue;
}
else {
int hc = hiscount % 10;
strcpy(histar[hc], hischar);
//printf("Copying to index at %d: %s\n" ,hc, hischar);
hiscount++;
}
//Check variables to find out if commands is pipe or redirect
for(a = 0; a < n; a++){
if(!strcmp(params[a], "|")){
piped++; //pipe command entered
//printf("piped: %i", piped);
}
if(!strcmp(params[a], "<")){
redirin++; //redireted in command
//printf("redirin: %i", redirin);
}
if(!strcmp(params[a], ">")){
redirout++; //redirected out command
//printf("redirout: %i", redirout);
}
}
//If there is no need for pipe or redirection
if(piped == 0 && redirin == 0 && redirout == 0){
//printf("PARAM: %s\n", params[0]);
if(executeCmd(params) == 0) break;
}
else{
//If there is a need for pipe
if(piped != 0 ){
//Pipe function
do_pipe(params, piped);
}
//If there is redirection
else if(redirin == 1 || redirout == 1){
int ic1 = 2;
int ic2 = 4;
for(int i=0;params[i]!='\0';i++)
{
if(strcmp(params[i],"<")==0)
{
params[i]=NULL;
ic1 = i+1;
}
else if(strcmp(params[i],">")==0)
{
params[i]=NULL;
ic2 = i+1;
}
}
// Fork process
pid_t pid = fork();
// Error
if (pid == -1) {
char* error = strerror(errno);
printf("fork: %s\n", error);
return 1;
}
// Child process
else if (pid == 0) {
int in, out;
int p = 0;
char *args2[MAX_COMMAND_LENGTH + 1] = {};
for(p =0; p < ic1; p++){
args2[p] = params [p];
//printf("%s" , args2[p]);
}
args2[p] = NULL;
// Open input and output files
if(redirin == 1){
in = open(params[ic1], O_RDONLY);
//Error handling if the file does not exist.
if(in == -1){
printf("The file does not exist: %s\n", params[ic1]);
piped = 0;
redirin = 0;
redirout = 0;
close(in);
break;
}
// Replace standard input with input file
dup2(in, 0);
close(in);
}
if(redirout == 1){
out = open(params[ic2], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
// replace standard output with output file
dup2(out, 1);
close(out);
}
// execute arguments
execvp(args2[0], args2);
// Error occurred
char* error = strerror(errno);
printf("Error! azarifog: %s: %s\n", params[0], error);
}
// Parent process
else {
// Wait for child process to finish
int childStatus;
waitpid(pid, &childStatus, 0);
}
} // redirect
}
//reset variables
piped = 0;
redirin = 0;
redirout = 0;
//For the signal handler testing
//sleep(1);
}
return 0;
}
//To execute commands without pipe and redirection
int executeCmd(char** params){
// Fork process
pid_t pid = fork();
// Error
if (pid == -1) {
char* error = strerror(errno);
printf("Error! fork: %s\n", error);
return 1;
}
// Child process
else if (pid == 0) {
// Execute command
execvp(params[0], params);
// Error occurred
char* error = strerror(errno);
printf("Error! azarifog: %s: %s\n", params[0], error);
return 0;
}
// Parent process
else {
// Wait for child process to finish
int childStatus;
waitpid(pid, &childStatus, 0);
return 1;
}
}
//Signal handler
void sighandler(int signum){
printf("Signal caught, exiting...\n");
exit(1);
}