-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
398 lines (342 loc) · 8.14 KB
/
Copy pathmyshell.c
File metadata and controls
398 lines (342 loc) · 8.14 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
/* A project implemented for university course
Operating Systems at 7th semester, Aristotle
University of Thessaloniki, Greece, 07/01/2019
by Vasileios Dimitriadis 8404 */
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LINESIZE 512 // This the maximum number of characters in a line.
#define TOK_DELIM " \t\r\n\a"
/* Shell modes are the interactive and the batch mode.
In interactive mode the user can type commands that
wants to be executed using the prompt. In batch mode
the user should provide a batchfile that has commands
written in it and all of these commands will be executed.
*/
void interactive_mode();
void batch_mode(char const *batchfileName);
// Functions
void prompt();
char *getLine();
char **getArgs(char *line);
int simpleExecute(char **args, int waitCmd);
int execute(char **args);
char *getFileLine(FILE *fp);
int myshell_cd(char **args);
int myshell_help(char **args);
int main(int argc, char const *argv[])
{
// Check number of arguments to decide in which mode to run
if(argc == 1)
{
// Run shell in Interactive mode
interactive_mode();
}
else if(argc == 2)
{
// Run shell in Batch mode
batch_mode(argv[1]);
}
else
{
// Incorrect number of arguments
printf("Enter the correct number of arguments...\n");
printf("Enter no arguments to run shell in interactive mode\n");
printf("OR enter the name of a batchfile to run shell in batch mode!!!\n");
}
return 0;
}
/* This shell has some built-in commands that need to
be implemented differently than the regular ones that
are already functions of Linux. These two built-in
commands are 'cd' that is used to change directory and
'help' that is used to provide info about myshell.
*/
int (*builtin_func[]) (char **) = {&myshell_cd, &myshell_help};
char *builtin_str[] = {"cd", "help"};
/* The following function is used to calculate the
number of built-in commands of myshell as there
is a possibility to extend them. */
int num_builtins()
{
return sizeof(builtin_str) / sizeof(char *);
}
int myshell_cd(char **args)
{
if (args[1] == NULL)
{
printf("Expected argument to \"cd\"...\n");
exit(EXIT_FAILURE);
}
else
{
if(chdir(args[1]) != 0)
{
printf("Error using cd command");
exit(EXIT_FAILURE);
}
}
return 0;
}
int myshell_help(char **args)
{
int i;
printf("This is a new shell!\n");
printf("Type commands and arguments and hit enter.\n");
printf("The followings are builtin:\n");
for (i=0; i<num_builtins(); i++)
{
printf("%s ", builtin_str[i]);
}
printf("\n");
printf("To exit this shell type 'quit'\n");
return 0;
}
// This is what is being displayed on the prompt.
void prompt()
{
printf("dimitriadis_8404> ");
}
// Read the line that there is at prompt.
char *getLine()
{
int bufsize = LINESIZE;
char *line = malloc(bufsize * sizeof(char));
if(line == NULL)
{
printf("Allocation error in function getLine...\n");
exit(EXIT_FAILURE);
}
fflush(stdin);
if (fgets(line, bufsize, stdin) == NULL)
{
printf("Can't read more than %d characters at one line...\n", bufsize);
exit(EXIT_FAILURE);
}
return line;
}
// Split the line into arguments as to manage the commands
// that are given more easily.
char **getArgs(char *line)
{
int bufsize = LINESIZE;
int index = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;
if(tokens == NULL)
{
printf("Can't allocate memory for tokens...\n");
exit(EXIT_FAILURE);
}
token = strtok(line, TOK_DELIM);
while(token != NULL)
{
tokens[index] = token;
index++;
token = strtok(NULL, TOK_DELIM);
}
tokens[index] = NULL;
return tokens;
}
/* This is the function that executes all the commands that
are given. It forks the process and the child process is
the one that executes the process while the parent process
is waiting for it.
*/
int simpleExecute(char **args, int waitCmd)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0) // Child process
{
if (execvp(args[0], args) == -1)
{
printf("Invalid argument \'%s\'.\n", args[0]);
exit(EXIT_FAILURE);
}
else
{
return 0;
}
}
else if (pid < 0) // Error forking
{
printf("Error forking...\n");
exit(EXIT_FAILURE);
}
else // Parent process
{
do
{
wpid = waitpid(pid, &status, WUNTRACED);
if (wpid == -1)
{
printf("Error in waiting pid...\n");
exit(EXIT_FAILURE);
}
}while(!WIFEXITED(status) && !WIFSIGNALED(status));
if (waitCmd == 1)
{
if (WEXITSTATUS(status) == 0)
{
return 0;
}
else
{
printf("Arguments following \'%s\' aren't executed...\n", args[0]);
return EXIT_FAILURE;
}
}
else if (waitCmd == 0)
{
return 0; // Don't care if previous command executed successfully or not.
}
else
{
printf("Invalid waitCmd value:%d...\n", waitCmd);
exit(EXIT_FAILURE);
}
}
return 0;
}
int execute(char **args)
{
int i = 0;
int waitCmd = 0;
int j, status, no_builtin;
char **simpleArgs = malloc(LINESIZE * sizeof(char*));
if(simpleArgs == NULL)
{
printf("Can't allocate memory for simpleArgs...\n");
exit(EXIT_FAILURE);
}
while (args[i] != NULL)
{
// first check if user wants to quit our shell.
if (strcmp(args[i], "quit") == 0)
{
printf("You are exiting this shell...\n");
printf("Thanks for using! Goodbye!!!\n");
free(simpleArgs);
return -1;
}
// if there are multiple user commands
// split them into single commands and then execute them.
j = 0;
do
{
simpleArgs[j] = strdup(args[i]);
j++;
i++;
}while(args[i] != NULL && strcmp(args[i], ";") != 0 && strcmp(args[i], "&&") != 0);
simpleArgs[j] = NULL;
if (args[i] != NULL)
{
if (strcmp(args[i], ";") == 0)
{
waitCmd = 0; // don't wait for the previous command to finish
}
if (strcmp(args[i], "&&") == 0)
{
waitCmd = 1; // wait for previous command to finish and execute only if returned successfully
}
i++;
}
no_builtin = 1; // This is used as a way to know if the command is built-in or regular one.
// We check if the command is a built-in so as to be executed in this stage
// and not to be forked and executed by a child process.
for(j = 0; j < num_builtins(); j++)
{
if(strcmp(simpleArgs[0], builtin_str[j]) == 0)
{
no_builtin = 0; // so we know it is a built-in
status = (*builtin_func[j])(simpleArgs);
}
}
if(no_builtin)
{
status = simpleExecute(simpleArgs, waitCmd);
}
// free allocated memory
j = 0;
while (simpleArgs[j] != NULL) {
free(simpleArgs[j++]);
}
if (status != 0)
{
break;
}
}
// Free memory
free(simpleArgs);
return 0;
}
void interactive_mode()
{
char *line;
char **args;
int status;
do
{
prompt();
line = getLine();
args = getArgs(line);
status = execute(args);
free(line);
free(args);
}while(status != -1);
}
// Read the line of a file.
char *getFileLine(FILE *fp)
{
int bufsize = LINESIZE;
char *line = malloc(bufsize * sizeof(char));
if(line == NULL)
{
printf("Allocation error in function getFileLine...\n");
exit(EXIT_FAILURE);
}
if(fgets(line, bufsize, fp) == NULL)
{
if(feof(fp))
{
strcpy(line, "quit");
}
else
{
printf("Can't read more than %d characters in a line...\n", bufsize);
exit(EXIT_FAILURE);
}
}
fflush(fp);
return line;
}
void batch_mode(char const *batchfileName)
{
FILE *fp;
char *line;
char **args;
int status;
prompt();
printf("Executing commands in file '%s' line by line\n", batchfileName);
fflush(stdout);
fp = fopen(batchfileName, "r");
if(fp == NULL)
{
printf("Can't open batchfile %s...\n", batchfileName);
exit(EXIT_FAILURE);
}
do
{
printf("\n");
line = getFileLine(fp);
args = getArgs(line);
status = execute(args);
free(line);
free(args);
}while(status != -1);
fclose(fp);
}