-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathminiShell.c
More file actions
431 lines (376 loc) · 10.2 KB
/
miniShell.c
File metadata and controls
431 lines (376 loc) · 10.2 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*******************************************************************
* File: miniShell.c
* Author: Ashwitha Yadav T
* Date Started: September 22, 2017
* Description: A simple implementation of the Unix Shell in the
* C Programming language.
*******************************************************************/
#include <stdio.h> // For: printf(), fprintf(), getchar(), perror(), stderr
#include <stdlib.h> // For: malloc(), realloc(), free(), exit(), execvp(), EXIT_SUCCESS, EXIT_FAILURE
#include <string.h> // For: strtok(), strcmp(), strcat(), strcpy()
#include <unistd.h> // For: chdir(), fork(), exec(), pid_t, getcwd()
#include <sys/wait.h> // For: waitpid()
#include <fcntl.h> // For: File creation modes
#define BUILTIN_COMMANDS 5 // Number of builtin commands defined
/*
* Environment variables
*/
char PWD[1024]; // Present Working Directory
char PATH[1024]; // Path to find the commands
/*
* Built-in command names
*/
char * builtin[] = {"cd", "exit", "help", "pwd", "echo"};
/*
* Built-in command functions
*/
/*
* Function: shell_cd
* -------------------
* changes current working directory
*
* args: arguments to the cd command, will consider only the first argument after the command name
*/
int shell_cd(char ** args){
if (args[1] == NULL){
fprintf(stderr, "minsh: one argument required\n");
}
else if (chdir(args[1]) < 0){
perror("minsh");
}
getcwd(PWD, sizeof(PWD)); // Update present working directory
return 1;
}
/*
* Function: shell_exit
* ---------------------
* exits from the shell
*
* return: status 0 to indicate termination
*/
int shell_exit(char ** args){
return 0;
}
/*
* Function: shell_help
* ---------------------
* prints a small description
*
* return: status 1 to indicate successful termination
*/
int shell_help(char ** args){
printf("\nA mini implementation of the Unix Shell by Ashwitha Yadav T.\n");
printf("\nCommands implemented: ");
printf("\n\t- help");
printf("\n\t- exit");
printf("\n\t- cd dir");
printf("\n\t- pwd");
printf("\n\t- echo [string to echo]");
printf("\n\t- clear");
printf("\n\t- ls [-ail] [dir1 dir2 ...]");
printf("\n\t- cp source target (or) cp file1 [file2 ...] dir");
printf("\n\t- mv source target (or) mv file1 [file2 ...] dir");
printf("\n\t- rm file1 [file2 ...]");
printf("\n\t- mkdir dir1 [dir2 ...]");
printf("\n\t- rmdir dir1 [dir2 ...]");
printf("\n\t- ln [-s] source target");
printf("\n\t- cat [file1 file2 ...]");
printf("\n\n");
printf("Other features : ");
printf("\n\t* Input, Output and Error Redirection (<, <<, >, >>, 2>, 2>> respectively) : ");
printf("\n\t* Example: ls -i >> outfile 2> errfile [Space mandatory around redirection operators!]");
printf("\n\n");
return 1;
}
/*
* Function: shell_pwd
* --------------------
* prints the present working directory
*
* return: status 1 to indicate successful termination
*/
int shell_pwd(char ** args){
printf("%s\n", PWD);
return 1;
}
/*
* Function: shell_echo
* ---------------------
* displays the string provided
*
* return: status 1 to indicate successful termination
*/
int shell_echo(char ** args){
int i = 1;
while (1){
// End of arguments
if (args[i] == NULL){
break;
}
printf("%s ", args[i]);
i++;
}
printf("\n");
}
/*
* Array of function pointers to built-in command functions
*/
int (* builtin_function[]) (char **) = {
&shell_cd,
&shell_exit,
&shell_help,
&shell_pwd,
&shell_echo
};
/*
* Function: split_command_line
* -----------------------------
* splits a commandline into tokens using strtok()
*
* command: a line of command read from terminal
*
* returns: an array of pointers to individual tokens
*/
char ** split_command_line(char * command){
int position = 0;
int no_of_tokens = 64;
char ** tokens = malloc(sizeof(char *) * no_of_tokens);
char delim[2] = " ";
// Split the command line into tokens with space as delimiter
char * token = strtok(command, delim);
while (token != NULL){
tokens[position] = token;
position++;
token = strtok(NULL, delim);
}
tokens[position] = NULL;
return tokens;
}
/*
* Function: read_command_line
* ----------------------------
* reads a commandline from terminal
*
* returns: a line of command read from terminal
*/
char * read_command_line(void){
int position = 0;
int buf_size = 1024;
char * command = (char *)malloc(sizeof(char) * 1024);
char c;
// Read the command line character by character
c = getchar();
while (c != EOF && c != '\n'){
command[position] = c;
// Reallocate buffer as and when needed
if (position >= buf_size){
buf_size += 64;
command = realloc(command, buf_size);
}
position++;
c = getchar();
}
return command;
}
/*
* Function: start_process
* ------------------------
* starts and executes a process for a command
*
* args: arguments tokenized from the command line
*
* return: status 1
*/
int start_process(char ** args){
int status;
pid_t pid, wpid;
pid = fork();
if (pid == 0){ // It's the child process
// Find the path of the command
char cmd_dir[1024];
strcpy(cmd_dir, PATH);
strcat(cmd_dir, args[0]);
// Execute the required process
if ( execv( cmd_dir, args ) == -1){ // Error
perror("minsh");
}
exit(EXIT_FAILURE); // To exit from child process
}
else if (pid < 0){ // Error in forking
perror("minsh");
}
else{ // It's the parent process
do{
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
/*
* Function: shell_execute
* ------------------------
* determines and executes a command as a built-in command or an external command
*
* args: arguments tokenized from the command line
*
* return: return status of the command
*/
int shell_execute(char ** args){
if (args[0] == NULL){ // Empty command
return 1;
}
// Copy the current Standard Input and Output file descriptors
// so they can be restored after executing the current command
int std_in, std_out, std_err;
std_in = dup(0);
std_out = dup(1);
std_err = dup(2);
// Check if redirection operators are present
int i = 1;
while ( args[i] != NULL ){
if ( strcmp( args[i], "<" ) == 0 ){ // Input redirection
int inp = open( args[i+1], O_RDONLY );
if ( inp < 0 ){
perror("minsh");
return 1;
}
if ( dup2(inp, 0) < 0 ){
perror("minsh");
return 1;
}
close(inp);
args[i] = NULL;
args[i+1] = NULL;
i += 2;
}
else if ( strcmp( args[i], "<<" ) == 0 ){ // Input redirection
int inp = open( args[i+1], O_RDONLY );
if ( inp < 0 ){
perror("minsh");
return 1;
}
if ( dup2(inp, 0) < 0 ){
perror("minsh");
return 1;
}
close(inp);
args[i] = NULL;
args[i+1] = NULL;
i += 2;
}
else if( strcmp( args[i], ">") == 0 ){ // Output redirection
int out = open( args[i+1], O_WRONLY | O_TRUNC | O_CREAT, 0755 );
if ( out < 0 ){
perror("minsh");
return 1;
}
if ( dup2(out, 1) < 0 ){
perror("minsh");
return 1;
}
close(out);
args[i] = NULL;
args[i+1] = NULL;
i += 2;
}
else if( strcmp( args[i], ">>") == 0 ){ // Output redirection (append)
int out = open( args[i+1], O_WRONLY | O_APPEND | O_CREAT, 0755 );
if ( out < 0 ){
perror("minsh");
return 1;
}
if ( dup2(out, 1) < 0 ){
perror("minsh");
return 1;
}
close(out);
args[i] = NULL;
args[i+1] = NULL;
i += 2;
}
else if( strcmp( args[i], "2>") == 0 ){ // Error redirection
int err = open( args[i+1], O_WRONLY | O_CREAT, 0755 );
if ( err < 0 ){
perror("minsh");
return 1;
}
if ( dup2(err, 2) < 0 ){
perror("minsh");
return 1;
}
close(err);
args[i] = NULL;
args[i+1] = NULL;
i += 2;
}
else if( strcmp( args[i], "2>>") == 0 ){ // Error redirection
int err = open( args[i+1], O_WRONLY | O_CREAT | O_APPEND, 0755 );
if ( err < 0 ){
perror("minsh");
return 1;
}
if ( dup2(err, 2) < 0 ){
perror("minsh");
return 1;
}
close(err);
args[i] = NULL;
args[i+1] = NULL;
i += 2;
}
else{
i++;
}
}
// If the command is a built-in command, execute that function
for(i = 0 ; i < BUILTIN_COMMANDS ; i++){
if ( strcmp(args[0], builtin[i]) == 0 ){
int ret_status = (* builtin_function[i])(args);
// Restore the Standard Input and Output file descriptors
dup2(std_in, 0);
dup2(std_out, 1);
dup2(std_err, 2);
return ret_status;
}
}
// For other commands, execute a child process
int ret_status = start_process(args);
// Restore the Standard Input and Output file descriptors
dup2(std_in, 0);
dup2(std_out, 1);
dup2(std_err, 2);
return ret_status;
}
/*
* Function: shell_loop
* ---------------------
* main loop of the Mini-Shell
*/
void shell_loop(void){
// Display help at startup
int status = shell_help(NULL);
char * command_line;
char ** arguments;
status = 1;
while (status){
printf("minsh> ");
command_line = read_command_line();
if ( strcmp(command_line, "") == 0 ){
continue;
}
arguments = split_command_line(command_line);
status = shell_execute(arguments);
}
}
/*
* Function: main
*/
int main(int argc, char ** argv){
// Shell initialization
getcwd(PWD, sizeof(PWD)); // Initialize PWD Environment Variable
strcpy(PATH, PWD); // Initialize the command PATH
strcat(PATH, "/cmds/"); // ^^
// Main loop of the shell
shell_loop();
return 0;
}