-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
331 lines (311 loc) · 11 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
// for standard input output
#include <stdio.h>
// header for fork(), getcwd()
#include <unistd.h>
// for string manipulations
#include <string.h>
// for printing EXIT_FAILURE, etc
#include <stdlib.h>
// header for waitpid()
#include <sys/wait.h>
// for errno
#include <errno.h>
// global define
#define MAXCOM 1000
#define LSH_TOK_BUFSIZE 64
#define LSH_TOK_DELIM " \t\r\n\a"
// // builtin commands list
// char* builtins[] = {
// "exit",
// "help",
// "wget",
// "pmap",
// "ps"
// };
// // number of builtin commands
// int numBuiltins = sizeof(builtins)/sizeof(char*);
// initial function when shell starts printing copyright rights
void initShell(){
printf("\n\n\n");
printf("################################################\n");
printf("\n");
printf(" (c) Shruti Katpara \n");
printf("\n");
printf("################################################\n");
printf("\n\n\n");
}
// end of initial function
// function to write prompt line and read the input command
char* Prompt(){
char currDir[1024];
// to store current directory path string in currDir array using getcwd()
getcwd(currDir,sizeof(currDir));
// printing currDir in prompt
printf("$$$:%s ",currDir);
// to store input line
char* line = NULL;
ssize_t bufsize = 0; // have getline allocate a buffer for us
// checking if the getline() reads EOF or null and returning failure
if (getline(&line, &bufsize, stdin) == -1){
// checking for EOF of given input
if (feof(stdin)) {
exit(EXIT_SUCCESS); // We recieved an EOF
}
// if its not EOF, then there will error in reading the input
else{
perror("readline");
exit(EXIT_FAILURE);
}
}
// returning input command
return line;
}
// end of Prompt()
// function to tokenize the input command
char **tokenize(char *line)
{
int bufsize = LSH_TOK_BUFSIZE, position = 0;
char **tokens = malloc(bufsize * sizeof(char*));
char *token;
// if memory allocation fails
if (!tokens) {
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
token = strtok(line, LSH_TOK_DELIM);
while (token != NULL) {
tokens[position] = token;
position++;
if (position >= bufsize) {
bufsize += LSH_TOK_BUFSIZE;
tokens = realloc(tokens, bufsize * sizeof(char*));
// if realloc fails
if (!tokens) {
printf("%s\n", strerror(errno));
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, LSH_TOK_DELIM);
}
tokens[position] = NULL;
return tokens;
}
// end of tokenize()
// cd command function implementation
// for cd we dont have to fork the shell process
// because in cd we have to change the directory in current shell process
int cd(char* args[]){
char s[1024];
char* argument = "cd <nameOfDirectory>";
if (args[1]==NULL){
printf("Expected argument %s\n",argument);
}
else{
if (chdir(args[1]) != 0){
printf("%s\n", strerror(errno));
}
}
return 1;
}
// end of cd() function
//function to execute the input commands
int execute(char** args)
{
// if no arguments were passed
if (args[0]==NULL){
return 1;
}
else{
// strcmp() will compare the first argument with "Cd"
// returns 0 on success
// for cd we dont have to fork the shell process
// because in cd we have to change the directory in current shell process
if (strcmp("Cd", args[0])==0){
int temp = cd(args);
}
else {
// forking the process
int pid = fork();
//int len = sizeof(args)/sizeof(char);
//printf("%s\n",args[-1]);
// if last argument is &
// then exit from the parent process
// because of this child will be orphaned now
// which means it will execute on it own's in background
// the command will be given back to shell
// if (strcmp(args[len-1],"&")==0){
// if (pid==0){
// args[len-1] = NULL;
// // check using strcmp() which command is to executed
// // using execvp(), execute the binary file of that command
// // on error, it returns -1
// if (strcmp(args[0], "ls") == 0){
// if (execvp("./ls",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "Mkdir") == 0){
// if (execvp("./mkdir",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "Cp") == 0){
// if (execvp("./Cp",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "Mv") == 0){
// if (execvp("./mv",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "Pwd") == 0){
// if (execvp("./pwd",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "Chmod") == 0){
// if (execvp("./chmod",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "rm") == 0){
// if (execvp("./rm",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "cat") == 0){
// if (execvp("./cat",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else if (strcmp(args[0], "grep") == 0){
// if (execvp("./grep",args) < 0){
// printf("%s\n", strerror(errno));
// }
// exit(EXIT_FAILURE);
// }
// else{
// if(execvp(args[0],args)<0){
// printf("%s\n",strerror(errno));
// }
// }
// }
// else if (pid>0){
// exit(EXIT_SUCCESS);
// }
// else{
// printf("%s\n", strerror(errno));
// }
// }
// else{
if (pid == 0) {
// Child process
// check using strcmp() which command is to executed
// using execvp(), execute the binary file of that command
// on error, it returns -1
if (strcmp(args[0], "ls") == 0){
if (execvp("./ls",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "Mkdir") == 0){
if (execvp("./mkdir",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "Cp") == 0){
if (execvp("./Cp",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "Mv") == 0){
if (execvp("./mv",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "Pwd") == 0){
if (execvp("./pwd",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "Chmod") == 0){
if (execvp("./chmod",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "rm") == 0){
if (execvp("./rm",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "cat") == 0){
if (execvp("./cat",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else if (strcmp(args[0], "grep") == 0){
if (execvp("./grep",args) < 0){
printf("%s\n", strerror(errno));
}
exit(EXIT_FAILURE);
}
else{
if(execvp(args[0],args)<0){
printf("%s\n",strerror(errno));
}
}
}
else if (pid < 0) {
// error in forking the process
printf("%s\n", strerror(errno));
}
else {
// Parent process
// using wait(NULL), parent process waits for its all children to finish processing
// and then executes.
int rc_wait = wait(NULL);
}
//}
}
// return status of 1 showing all execution was successful and prompt should continue
return 1;
}
}
// end of execute()
// main function
int main(){
char* inputString;
char **args;
int status;
// printing copyright
// this will be executed everytime one runs this shell
initShell();
// shell continues untill there is successful execution of input commands.
do {
inputString = Prompt();
args = tokenize(inputString);
status = execute(args);
// freeing up allocated memory.
free(inputString);
free(args);
}
while(status);
return 0;
}