-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
408 lines (347 loc) · 10.4 KB
/
main.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
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<wait.h>
#include<pthread.h>
// commands included -- cd, echo, pwd, exit, mkdir, rm, date, cat, ls
char** arguments;
int count = 0;
char* comm;
int comm_i = 0;
char** arg;
void make_arguments(char* line){
char* token = strtok(line, " \n");
while(token != NULL){
// if(strcmp(token, "") == 0){
// printf("Hello");
// token = strtok(NULL, " \n");
// continue;
// }
arguments[count] = token;
token = strtok(NULL, " \n");
count++;
}
free(token);
}
void cd_L(char* path){
int val = chdir(path);
if(val == -1){
perror(" Error ");
return;
}
}
void cd_P(char* path){
char a[100];
char* real_path = realpath(path, a);
int val = chdir(a);
if(val == -1){
perror(" Error ");
return;
}
}
void cd_help(){
printf("cd: cd [-L|[-P [-e]] [-@]] [dir] \nChange the shell working directory. \n\nChange the current directory to DIR. The default DIR is the value of the\nHOME shell variable.\n\nThe variable CDPATH defines the search path for the directory containing\nDIR. Alternative directory names in CDPATH are separated by a colon (:).\nA null directory name is the same as\nthe current directory. If DIR begins\nwith a slash (/), then CDPATH is not used.\n\nIf the directory is not found, and\nthe shell option cdable_vars is set,\nthe word is assumed to be a variable name. If that variable has a value,\nits value is used for DIR.\n\nOptions:\n -L force symbolic links to be followed: resolve symbolic\n links in DIR after processing instances of '..');\n -P use the physical directory structure without following\n symbolic links: resolve symbolic links in DIR before\n processing instances of '..'\n -e if the -P option is supplied, and the current working\n directory cannot be determined successfully, exit with\n a non-zero status\n -@ on systems that support it, present a file with extended\n attributes as a directory containing the file attributes\n\nThe default is to follow symbolic links, as if '-L' were specified.\n'..' is processed by removing the immediately previous pathname component\nback to a slash or the beginning of DIR.\n\nExit Status:\nReturns 0 if the directory is changed, and if $PWD is set successfully when\n-P is used; non-zero otherwise. \n");
}
void cd(int val){
char* temp = arguments[1 + val];
if(temp != NULL){
if(temp[0] == '-'){
if(strcmp(temp, "-L") == 0){
temp = arguments[2 + val];
if(temp != NULL){
cd_L(temp);
}
else{
printf("Invalid Input");
}
}
else if(strcmp(temp, "-P") == 0){
temp = arguments[2 + val];
if(temp != NULL){
cd_P(temp);
}
else{
printf("Invalid Input");
}
}
else if(strcmp(temp, "--help") == 0){
cd_help();
}
else{
printf("Invalid Input -- %s \n", temp);
}
}
else{
if(chdir(temp) == -1){
perror(" Error ");
return;
}
}
}
else{
if(chdir(getenv("HOME")) == -1){
perror(" Error "); // here error should be corrected depending on the error which should come
return;
}
}
}
// The default DIR is the value of the HOME shell variable.
void echo_n(int val){
for(int i=2+val;i<count;i++){
printf("%s ", arguments[i]);
}
}
void echo_E(int val){
for(int j=2+val;j<count;j++){
char* output = arguments[j];
for(int i=0;i<strlen(output);i++){
if(output[i] == '\0'){
break;
}
else if(output[i] == '/'){ // instead of \ we have used /
continue;
}
else{
printf("%c", output[i]);
}
}
}
printf("\n");
}
void echo(int val){
char* temp = arguments[1 + val];
// token = strtok(NULL, " \n");
if(temp != NULL){
if(temp[0] == '-'){
if(strcmp(temp, "-n") == 0){
temp = arguments[2 + val];
if(temp != NULL){
echo_n(val);
}
else{
printf("Invalid Input");
}
}
else if(strcmp(temp, "-E") == 0){
temp = arguments[2 + val];
if(temp != NULL){
echo_E(val);
}
else{
printf("Invalid Input");
}
}
else{
printf("Invalid token -- %s \n", temp);
}
}
else{
for(int i=1 + val;i<count;i++){
printf("%s ", arguments[i]);
}
printf("\n");
}
}
else{
printf("\n");
}
}
void pwd_L(){
char* line = malloc(sizeof(char)*100);
size_t n = 100;
getcwd(line, n);
printf("%s \n", line);
free(line);
}
void pwd_P(){
char* line = malloc(sizeof(char)*100);
size_t n = 100;
getcwd(line, n);
printf("%s \n", line);
free(line);
}
void pwd_help(){
printf("pwd: pwd [-LP] \n\tPrint the name of the current working directory.\n \n\tOptions: \n\t-L print the value of $PWD\n\tif it names the current working\n\t\tdirectory\n\t-P print the physical directory, without any symbolic links\n\n\tBy default, `pwd' behaves as if `-L' were specified.\n\n\tExit Status:\n\n\tReturns 0 unless an invalid option is\n\tgiven or the current directory\n\tcannot be read.\n");
}
void pwd(int val){
char* temp = arguments[1 + val];
if(temp != NULL){
// if(strcmp(temp, "") == 0){
// pwd_L();
// }
if(strcmp(temp, "--help") == 0){
pwd_help();
}
else if(temp[0] == '-'){
if(strcmp(temp, "-L") == 0){
pwd_L();
}
else if(strcmp(temp, "-P") == 0){
pwd_P();
}
else{
printf("Invalid Input -- %s \n", temp);
}
}
else{
printf("Invalid Input");
}
}
else{
char* line = malloc(sizeof(char)*100);
size_t n = 100;
if(getcwd(line, n) == NULL){
printf("Error");
};
printf("%s \n", line);
free(line);
}
}
// This is for fork and execvp
void make_external_commands(char* file){
arg[0] = file;
for(int i=0;i<count;i++){
arg[i+1] = arguments[i];
}
pid_t pid = fork();
if(pid == 0){
int val = execvp(file, arg);
if(val == -1){
perror(" Error ");
}
}
else{
wait(NULL);
}
return;
}
// This is for pthreading -- pthread and system
void* make_call(void* argv){
if(system(comm) == -1){
printf("Error");
}
pthread_exit(0);
return NULL;
}
void make_external_commands_threads(char* file){
// appending file
for(int i=0;i<strlen(file);i++){
comm[comm_i++] = file[i];
}
// i = 1 for commands execution
for(int i=1;i<count;i++){
// for space
comm[comm_i++] = ' ';
for(int j=0;j<strlen(arguments[i]);j++){
comm[comm_i++] = arguments[i][j];
}
}
pthread_t pthread;
pthread_create(&pthread, NULL, make_call, NULL);
pthread_join(pthread, NULL);
free(comm);
}
// to free the arguments
void free_arguments(){
for(int i=0;i<100;i++){
comm[i] = ' ';
}
for(int i=0;i<=count;i++){
arg[i] = NULL;
}
for(int i=0;i<count;i++){
arguments[i] = NULL;
}
comm_i = 0;
count = 0;
}
int main(){
arguments = (char**)malloc(100*sizeof(char*));
arg = (char**)malloc(sizeof(char*)*100);
comm = malloc(sizeof(char)*100);
char* line = NULL;
size_t n = 0;
int val = 0; // this is for program to tell whether to use fork or pthreads
while(1){
printf("@>");
getline(&line, &n, stdin);
if(strcmp(line, "\n") == 0){
continue;
}
make_arguments(line);
if(strcmp(arguments[0], "&t") == 0){
// printf("In the pthread\n");
val = 1;
}
else{
val = 0;
}
// Internal commands
// echo
if(strcmp(arguments[0 + val], "echo") == 0){
echo(val);
}
// pwd
else if(strcmp(arguments[0 + val], "pwd")==0){
pwd(val);
}
// cd
else if(strcmp(arguments[0 + val], "cd") == 0){
cd(val);
}
// External commands
else if(strcmp(arguments[0 + val], "cat") == 0){
if(val == 1){
make_external_commands_threads("./cat");
}
else{
make_external_commands("./cat");
}
}
else if(strcmp(arguments[0 + val], "date") == 0){
if(val == 1){
make_external_commands_threads("./date");
}
else{
make_external_commands("./date");
}
}
else if(strcmp(arguments[0 + val], "ls") == 0){
if(val == 1){
make_external_commands_threads("./ls");
}
else{
make_external_commands("./ls");
}
}
else if(strcmp(arguments[0 + val],"mkdir") == 0){
if(val == 1){
make_external_commands_threads("./mkdir");
}
else{
make_external_commands("./mkdir");
}
}
else if(strcmp(arguments[0 + val], "rm") == 0){
if(val == 1){
make_external_commands_threads("./rm");
}
else{
make_external_commands("./rm");
}
}
else if(strcmp(arguments[0 + val], "exit") == 0){
break;
}
else{
printf("Wrong Command \n");
}
// clearing the arguments that was passed earlier
free_arguments();
}
free(arguments);
free(arg);
free(comm);
return 0;
}