-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200101070_Assign2.c
1226 lines (1194 loc) · 34 KB
/
200101070_Assign2.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <ctype.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <termios.h>
int noOfWords(char *command);
void shellPrompt();
static char *currentDirectory;
static char *currDirr;
int commandHandler(char *);
char* HOME;
char* SHELL;
char* TERM;
char* USER;
/********************************************************************************************************/
/********************************************************************************************************/
/**************************************DEALING WITH PIPES************************************************/
/**************************************CHECKING PIPE PRESENT***********************************************/
int pipeCheck(char *ptr)
{
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
int i = 0;
while (pn[i] != '\0')
{
if (pn[i] == '|')
{
return 1; // IF | SYMBOL PRESENT IT INCREASES BY ONE
}
i++;
}
free(pn);
return i; // RETURNING NO OF | symbol present
}
/********************************************************************************************************/
/*******************************IF PIPE PRESENT IMPLEMENT PIPE*******************************************/
void implementPipe(char *ptr)
{
char *pn = (char *)malloc(100 * sizeof(char));
char **token = (char **)malloc(3 * sizeof(char *));
char **tokenInput = (char **)malloc(40 * sizeof(char *));
char **tokenOutput = (char **)malloc(40 * sizeof(char *));
strcpy(pn, ptr);
int j = 0;
char *p = strtok(pn, "|");
// SEPERATING INTO TWO TOKEN ONE BEFORE PIPE ONE AFTER
while (p != NULL)
{
token[j] = p;
// printf("%s\n",token[j]);
j++;
p = strtok(NULL, "|");
}
//printf("\n");printf("\n");
token[j] = NULL;
strcpy(pn, ptr);
j = 0;
/* while(token[j]!=NULL)
{ // JUST FOR CHECK WHETHER IT IS SEPERATING PROPERLY OR NOT
printf("%s ",token[--j]);
j++;
}
printf("\n");
j = 0;
*/
// STORING AND SEPERATING THE BEFORE | STRING AND DIVIDING INTO WORDS
p = strtok(pn, "|");
// printf("%s\n\n",p);
char *q = strtok(p, " ");
while (q != NULL)
{
tokenInput[j] = q;
// printf("%s\n",tokenInput[j]);
j++;
q = strtok(NULL, " ");
}
tokenInput[j] = NULL;
j = 0;
// JUST FOR CHECK WHETHER IT IS SEPERATING PROPERLY OR NOT
/* while(tokenInput[j]!=NULL)
{
printf("%s\n",tokenInput[j]);
j++;
}
if(tokenInput[j]==NULL)
{
printf("0\n\n");
}
printf("\n");
printf("\n");
*/
//strcpy(pn,ptr);
// STORING AND SEPERATING THE AFTER | STRING AND DIVIDING INTO WORDS
j = 0;
q = strtok(token[1], " ");
while (q != NULL)
{
tokenOutput[j] = q;
// printf("%s\n",tokenOutput[j]);
j++;
q = strtok(NULL, " ");
}
tokenOutput[j] = NULL;
/* if(tokenOutput[j]==NULL)
{
printf("0\n\n");
}
*/
j = 0;
// strcpy(pn,ptr);
int h = 0;
/* while(tokenInput[h]!= NULL)
{
printf("%s\n",tokenInput[h]);
h++;
}
printf("\n");
*/
// NOW IMPLEMENETING LOGIC OF FILE DESCRIPTORS AND IMPLEMENTING FORK
int fd[2];
if (pipe(fd) == -1)
{
return;
}
int pid1 = fork();
if (pid1 < 0)
{
return;
}
if (pid1 == 0)
{
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
if (execvp(tokenInput[0], tokenInput) < 0)
{
printf("U ARE NOT GIVING RIGHT VALUES BEFORE PIPE\n");
exit(0);
}
execvp(tokenInput[0], tokenInput);
}
int pid2 = fork();
if (pid2 < 0)
{
return;
}
if (pid2 == 0)
{
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
if (execvp(tokenOutput[0], tokenOutput) < 0)
{
printf("U ARE NOT GIVING RIGHT VALUES AFTER PIPE\n");
exit(0);
}
execvp(tokenOutput[0], tokenOutput);
exit(0);
}
close(fd[0]);
close(fd[1]);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
free(pn);
free(tokenInput);
free(tokenOutput);
free(token);
return;
}
/********************************************************************************************************/
/********************************************************************************************************/
/********************************************************************************************************/
// FOR CHECKING IF OUTPUT REDIRECTION IS THERE
int RedirectionOutputCheck(char *ptr)
{
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
int i = 0;
int k = 0;
// FINDING HOW MANY TIMES > SYMBOL CAME
while (pn[i] != '\0')
{
if (pn[i] == '>')
{
k++;
}
i++;
}
free(pn);
return k;
}
// FOR CHECKING IF INPUT REDIRECTION IS THERE
int RedirectionInputCheck(char *ptr)
{
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
int i = 0;
int k = 0;
// FINDING HOW MANY TIMES < SYMBOL CAME
while (pn[i] != '\0')
{
if (pn[i] == '<')
{
k++;
}
i++;
}
free(pn);
return k;
}
// FOR CHECKING IF APPEND REDIRECTION IS THERE
int RedirectionAppendCheck(char *ptr)
{
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
int i = 0;
int k = 0;
// FINDING HOW MANY TIMES >> SYMBOL CAME
while (pn[i] != '\0')
{
if (pn[i] == '>')
{
k++;
}
i++;
}
free(pn);
return k;
}
// FOR IMPLEMETING OUTPUT OPERATION
void ImplementOutputRedirection(char *ptr)
{
// IF THE SYMBOL > PRESENT 1 TIME IMPLEMENTING THIS
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
char **token = (char **)malloc(3 * sizeof(char *));
int j = 0;
char **tokenInput = (char **)malloc(40 * sizeof(char *));
char *p = strtok(pn, ">");
// DIVIDING INTO TWO STRINGS BEFORE AND AFTER > USING TOKENS
while (p != NULL)
{
token[j] = p;
// printf("%s\n",token[j]); // JUST FOR CHECK WHETHER CORRECT OR NOT
j++;
p = strtok(NULL, ">");
}
token[j] = NULL;
strcpy(pn, ptr);
j = 0;
p = strtok(pn, ">");
// DIVISON INTO WORDS
char *q = strtok(p, " ");
while (q != NULL)
{
tokenInput[j] = q;
// printf("%s\n",tokenInput[j]); // JUST FOR CHECK WHETHER CORRECT OR NOT
j++;
q = strtok(NULL, " "); // DIVISON USING SPACE
}
tokenInput[j] = NULL;
j = 0;
// JUST FOR CHECK
/* { while (tokenInput[j] != NULL)
printf("%s\n",tokenInput[j]);
j++;
}
if(tokenInput[j]== NULL)
{
printf("0\n\n");
}
*/
// OPENING FILE AND PERFORMING OPERATION IN IT
int file = open(token[1], O_WRONLY | O_CREAT | O_TRUNC, 0666); //HERE I AM USING O_WRONLY,O_CREAT,O_TRUNC
if (file == -1)
{
printf("FILE NOT OPEN SUCCESSFULLY\n");
return;
}
/*if( file > -1) // JUST FOR CHECK
{
remove(token[1]);
file = open(token[1],O_WRONLY|O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO,0777);
}
*/
// IMPLEMENTING FORK
int pid1 = fork();
if (pid1 < 0)
{
return;
}
if (pid1 == 0)
{
dup2(file, 1);
close(file);
if (execvp(tokenInput[0], tokenInput) < 0)
{
exit(0);
}
execvp(tokenInput[0], tokenInput);
exit(0);
}
close(file);// CLOSING IT OTHERWISE IT WILL PRINT IN FILE ONLY
waitpid(pid1, NULL, 0);
free(pn);
free(token);
return;
}
// FOR IMPLEMETING INPUT OPERATION
void ImplementInputRedirection(char *ptr)
{
// OTHER INFO THAT I HAVE NOT COMMENTED SIMILIAR TO OUTPUT REDIRECTION
//ONLY DIFFERENCE HERE IS USE OF DIFFERENT FLAGS AND DIVISION ON BASIS OF <
// ALSO HERE FILE DESCRIPTOR CHANGED
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
char **token = (char **)malloc(3 * sizeof(char *));
int j = 0;
char **tokenInput = (char **)malloc(40 * sizeof(char *));
char *p = strtok(pn, "<");
while (p != NULL)
{
token[j] = p;
// printf("%s\n",token[j]);
j++;
p = strtok(NULL, "<");
}
token[j] = NULL;
strcpy(pn, ptr);
j = 0;
p = strtok(pn, "<");
char *q = strtok(p, " ");
while (q != NULL)
{
tokenInput[j] = q;
// printf("%s\n",tokenInput[j]);
j++;
q = strtok(NULL, " ");
}
tokenInput[j] = NULL;
/*j = 0;
while (tokenInput[j] != NULL)
{
printf("%s\n",tokenInput[j]);
j++;
}
if(tokenInput[j]== NULL)
{
printf("0\n\n");
}
*/
int file = open(token[1], O_RDONLY); //NOTE I AM USING HERE O_RDONLY
if (file == -1)
{
printf("CHECK AGAIN FILE NOT PRESENT\n");
return;
}
int pid1 = fork();
if (pid1 < 0)
{
return;
}
if (pid1 == 0)
{
dup2(file, 0); // HERE I AM USING 0 IN FILE DESCRIPTOR
close(file);
if (execvp(tokenInput[0], tokenInput) < 0)
{
exit(0);
}
execvp(tokenInput[0], tokenInput);
exit(0);
}
close(file);
waitpid(pid1, NULL, 0);
free(pn);
free(token);
return;
}
// FOR IMPLEMETING APPEND OPERATION
void ImplementAppendRedirection(char *ptr)
{
// OTHER INFO THAT I HAVE NOT COMMENTED SIMILIAR TO OUTPUT REDIRECTION
//ONLY DIFFERENCE HERE IS USE OF DIFFERENT FLAGS AND DIVISION ON BASIS OF >>
// ALSO HERE FILE DESCRIPTOR CHANGED
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
char **token = (char **)malloc(3 * sizeof(char *));
int j = 0;
char **tokenInput = (char **)malloc(40 * sizeof(char *));
char *p = strtok(pn, ">>");
while (p != NULL)
{
token[j] = p;
// printf("%s\n",token[j]);
j++;
p = strtok(NULL, ">>");
}
token[j] = NULL;
strcpy(pn, ptr);
j = 0;
p = strtok(pn, ">>");
char *q = strtok(p, " ");
while (q != NULL)
{
tokenInput[j] = q;
// printf("%s\n",tokenInput[j]);
j++;
q = strtok(NULL, " ");
}
tokenInput[j] = NULL;
j = 0;
/* while (tokenInput[j] != NULL)
{
printf("%s\n",tokenInput[j]);
j++;
}
if(tokenInput[j]== NULL)
{
printf("0\n\n");
}
*/
int file = open(token[1], O_CREAT | O_WRONLY | O_APPEND, 0666); //HERE I AM USING O_APPEND AND
// I REMOVED O_TRUNC
if (file == -1)
{
printf("FILE NOT OPEN SUCCESSFULLY\n");
return;
}
/*if( file > -1)
{
remove(token[1]);
file = open(token[1],O_WRONLY|O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO,0777);
}
*/
int pid1 = fork();
if (pid1 < 0)
{
return;
}
if (pid1 == 0)
{
dup2(file, 1);
close(file);
if (execvp(tokenInput[0], tokenInput) < 0)
{
exit(0);
}
execvp(tokenInput[0], tokenInput);
exit(0);
}
close(file);
waitpid(pid1, NULL, 0);
free(pn);
free(token);
return;
}
void ImplementBothRedirection(char *ptr)
{
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
char **token = (char **)malloc(3 * sizeof(char *));
int j = 0;
char **tokenInput = (char **)malloc(40 * sizeof(char *));
char *p = strtok(pn, "<");
while (p != NULL)
{
token[j] = p;
// printf("%s\n",token[j]);
j++;
p = strtok(NULL, "<");
}
token[j] = NULL;
strcpy(pn, ptr);
j = 0;
p = strtok(pn, "<");
char *q = strtok(p, " ");
while (q != NULL)
{
tokenInput[j] = q;
// printf("%s\n",tokenInput[j]);
j++;
q = strtok(NULL, " ");
}
tokenInput[j] = NULL;
/*j = 0;
while (tokenInput[j] != NULL)
{
printf("%s\n",tokenInput[j]);
j++;
}
if(tokenInput[j]== NULL)
{
printf("0\n\n");
}
*/
// printf("%s\n",token[1]);
char**tokenout = malloc(3*sizeof(char*));
char* duplicate = malloc(40*sizeof(char));
duplicate =strcpy(duplicate,token[1]);
// printf("%s\n",duplicate);
char*p2 = strtok(token[1],">");
//printf("%s\n",p2);
// printf("%s\n",duplicate);
q = strtok(duplicate,">");
q = strtok(NULL,">");
//printf("%s\n",q);
/* while (q != NULL)
{
tokenout[j] = q;
// printf("%s\n",tokenInput[j]);
j++;
q = strtok(NULL, " ");
}
tokenout[j] = NULL;
*/
int file = open(p2, O_RDONLY); //NOTE I AM USING HERE O_RDONLY
if (file == -1)
{
printf("CHECK AGAIN FILE NOT PRESENT\n");
return;
}
int file2= open(q, O_WRONLY | O_CREAT | O_TRUNC, 0666); //HERE I AM USING O_WRONLY,O_CREAT,O_TRUNC
if (file2 == -1)
{
printf("FILE NOT OPEN SUCCESSFULLY\n");
return;
}
int pid1 = fork();
if (pid1 < 0)
{
return;
}
if (pid1 ==0)
{
dup2(file, 0); // HERE I AM USING 0 IN FILE DESCRIPTOR
close(file);
dup2(file2,1);
close(file2);
if (execvp(tokenInput[0], tokenInput) < 0)
{
exit(0);
}
execvp(tokenInput[0], tokenInput);
exit(0);
}
close(file);
close(file2);
waitpid(pid1, NULL, 0);
free(pn);
free(token);
free(tokenout);
free(duplicate);
return;
/* // IMPLEMENTING SIMILIAR TO ABOVE
char *pn = (char *)malloc(100 * sizeof(char));
strcpy(pn, ptr);
char **token = (char **)malloc(4 * sizeof(char *));
int j = 0;
char **tokenInput = (char **)malloc(40 * sizeof(char *));
char *p = strtok(pn, ">");
while (p != NULL)
{
token[j] = p;
// printf("%s\n",token[j]); store output file name in token[1]
j++;
p = strtok(NULL, ">");
}
token[j] = NULL;
// strcpy(pn,ptr);
j = 0;
/* while (token[j] != NULL)
{
printf("%s\n",token[j]); store output file name in token[1]
j++;
}
*/
//ImplementInputRedirection(token[0]);
// ImplementInputRedirection(token[1]);
/*char *pn2 = (char *)malloc(100 * sizeof(char));
strcpy(pn2, token[0]);
char **token2 = (char **)malloc(3 * sizeof(char *));
int j2 = 0;
char **tokenInput2 = (char **)malloc(40 * sizeof(char *));
char **tokenInput3 = (char **)malloc(40 * sizeof(char *));
/* char *p2 = strtok(pn2, "<");
while (p2 != NULL)
{
token2[j2] = p2;
j2++;
p2 = strtok(NULL, "<");
}
token2[j2] = NULL;
j2 = 0;
printf("%s\n",token2[0]);
*/
/* char *q2 = strtok(token[0], "<");
while (q2 != NULL)
{
tokenInput2[j2] = q2;
//printf("%s\n",tokenInput2[j2]); store input filename in tokenInput[1]
j2++;
q2 = strtok(NULL, "<");
}
tokenInput2[j2] = NULL;
j2 = 0;
/* while (tokenInput2[j2] != NULL)
{
printf("%s\n",tokenInput2[j2]);
j2++;
}
if(tokenInput2[j2]== NULL)
{
printf("0\n\n");
}
*/
/* printf("%s\n%s\n%s\n", token[1], tokenInput2[0], tokenInput2[1]);
int file = open("input.txt", O_RDONLY | O_CREAT, 0666); //
if (file == -1)
{
printf("CHECK AGAIN FILE NOT PRESENT\n");
}
else
{
}
// printf("%s\n",k);
printf("%s\n", tokenInput2[1]);
int file2 = open(token[1], O_WRONLY | O_CREAT | O_TRUNC, 0666); //
if (file2 == -1)
{
printf("FILE NOT OPEN SUCCESSFULLY\n");
return;
}
else
{
}
int j3 = 0;
char *q3 = strtok(tokenInput2[0], " ");
while (q3 != NULL)
{
tokenInput3[j3] = q3;
// printf("%s\n",tokenInput3[j3]);
j3++;
q3 = strtok(NULL, " ");
}
tokenInput3[j3] = NULL;
j3 = 0;
*/
/* while (tokenInput3[j3] != NULL)
{
printf("%s\n",tokenInput3[j3]);
j3++;
}
if(tokenInput3[j3]== NULL)
{
printf("0\n\n");
}
*/
// close(file3);
/* int pid1 = fork();
if (pid1 < 0)
{
return;
}
if (pid1 == 0)
{
dup2(file, 0);
close(file);
if (execvp(tokenInput2[0], tokenInput2) < 0)
{
exit(0);
}
dup2(file2, 1);
close(file2);
execvp(tokenInput2[0], tokenInput2);
exit(0);
}
close(file2);
close(file);
waitpid(pid1, NULL, 0);
*/
/**************************************************************/
/* while (q != NULL)
{
tokenInput[j] = q;
// printf("%s\n",tokenInput[j]);
j++;
q = strtok(NULL, " ");
}
tokenInput[j] = NULL;
j = 0;
while (tokenInput[j] != NULL)
{
printf("%s\n",tokenInput[j]);
j++;
}
if(tokenInput[j]== NULL)
{
printf("0\n\n");
}
*/
/* int file = open(token[1],O_CREAT|O_WRONLY|O_APPEND,0666);//
if(file == -1)
{
printf("FILE NOT OPEN SUCCESSFULLY\n");
return;
}
/*if( file > -1)
{
remove(token[1]);
file = open(token[1],O_WRONLY|O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO,0777);
}
*/
/*int pid1 = fork();
if (pid1 < 0)
{
return ;
}
if (pid1 == 0)
{ dup2(file,1);
close(file);
if (execvp(tokenInput[0], tokenInput) < 0)
{
exit(0);
}
execvp(tokenInput[0], tokenInput);
exit(0);
}
close(file);
waitpid(pid1, NULL, 0);
free(pn);
free(pn2);
free(token);
free(tokenInput2);
free(tokenInput);
*/
return;
}
/********************************************************************************************************/
/********************************************************************************************************/
/*****************************************DEALING WITH HISTORY*******************************************/
/******************************************STORING HISTORY***********************************************/
void storeInput(char *command)
{
// STORING COMMAND
char *Directory = getcwd(currDirr, 1024);
//STORING DIRECTORY BECAUSE I HAVE TO CHANGE DIRECTORY AS history is in /tmp
FILE *fptr;
chdir("/tmp");
fptr = fopen("history.txt", "a+");
fprintf(fptr, "%s", command);
// STORING COMMAND IN HISTORY FILE LINE WISE
fprintf(fptr, "\n");
fclose(fptr);
// NOW AGAIN CHANGING TO PRESENT DIRECTORY
chdir(Directory);
return;
}
/*********************************RETRIEVING HISTORY***************************************/
void printHistory()
{
//STORING DIRECTORY BECAUSE I HAVE TO CHANGE DIRECTORY AS history is in /tmp
char *Directory = getcwd(currDirr, 1024);
//WE HAVE TO READ THE HISTORY FILE AND PRINT THE HISTORY
FILE *fptr;
chdir("/tmp");
fptr = fopen("history.txt", "r"); // READING A FILE
char c;
c = fgetc(fptr);
while (c != EOF)
{
printf("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
// NOW AGAIN CHANGING TO PRESENT DIRECTORY
chdir(Directory);
return;
}
/********************************************************************************************************/
/********************************************************************************************************/
/*******************************************NO OF WORDS**********************************************/
int noOfWords(char *command)
{ // FINDING NO OF WORDS
char *p = strtok(command, " ");
int j = 0;
while (p != NULL)
{
p = strtok(NULL, " ");
j++;
}
return j;
}
//TRYING TO IMPLEMENT PIPE BUT IT WAS GIVING SOME ERROR SO I LEFT HERE AND I HAVE IMPLEMENTED ABOVE
/* void gotopipe()
{ int i=0;
char*array[noOfWords+!];
char *p = strtok(command, " ");
while (p != NULL)
{
array[i++] = p;
p = strtok(NULL, " ");
}
int fd[2];
//fd[0] =read
//fd[1]=write
if(pipe(fd)==-1)
{
printf("AN ERROR OCURRED\n");
return 1;
}
int id = fork();
id(id == -1)
{
printf("AN ERROR OCURRED\n");
return 1;
}
if(id ==0)
{
close(fd[0]);
}
}*/
/***********************************************************************************************************/
/***********************************************************************************************************/
/********************************************PRINTING SHELL*************************************************/
void shellPrompt()
{
// We print the prompt in the form "<cwd> >"
printf("%s > ", getcwd(currentDirectory, 1024));
return;
}
/***********************************************************************************************************/
/***********************************************************************************************************/
/***********************************************************************************************************/
/***********************************************************************************************************/
/*************************************************MAIN******************************************************/
/***********************************************************************************************************/
int main()
{ // i was first trying to store path and seperating by : But it got some error so i am doing by other method
/* int noOfPaths = 0;
int e = 0;
while (pathcombined[e] != '\0')
{
if (pathcombined[e] == ':')
{
noOfPaths++;
}
e++;
}
printf("%d",noOfPaths);
char *arrayOfPaths[noOfPaths + 1];
int s= 0;
char *q = strtok(pathcombined, ":");
while (q != NULL)
{
arrayOfPaths[s++] = q;
q= strtok(NULL, ":");
}
printf("%s\n",arrayOfPaths[5]);
*/
SHELL= getenv("SHELL");
USER=getenv("USER");
HOME= getenv("HOME");
TERM= getenv("TERM");
int compare = 1;
char *command = (char *)malloc(100 * sizeof(char));
char *curr_path;
//int h = strcmp(command, "history");
curr_path = getcwd(curr_path, 100);
do
{
char **token = (char **)malloc(40 * sizeof(char *));//USEFUL FOR TOKEN ->DIVISION OF STRING INTO WORDS
command[0] = '\0';
int i = 0;
shellPrompt(); // PRINTING THE CURRENT PRESENT DIRECTORY
// SCANNING OF INPUT UNTIL U GET A NEW LINE CHARACTER
while (1)
{
char c;
scanf("%c", &c);
if (c == '\n')
{
command[i] = '\0'; // IF WE GET A NEW LINE CHARACTER REPLACE IT BY NULL CHARACTER
// WHICH WILL BE HELPFUL FOR STRING TERMINATION
break;
}
else
{
command[i] = c; // ELSE STORE CHARACTER
}
i++;
}
// IF INPUT CONTAINS NOTHING
if ((strcmp(command, "\0") == 0))
{ //DO NOT STORE
continue;
}
else
{
// ELSE STORE
storeInput(command);
}
// HERE I WANT TO REMAIN HAVING ACCESS TO THE REAL INPUT BECAUSE ON STRTOK IT WILL MODIFY COMMAND
// SO I DEFINED AND CREATED A POINTER THAT STORES EXACTLY SAME VALUE AS INPUT
char *ptr = (char *)malloc(100 * sizeof(char));
ptr = strcpy(ptr, command);
// I HAVE EARLIER DEFINED TOKEN SO I AM NOT DEFINING HERE
//char*token[40]; // USE FOR DIVIDING THE INPUT INTO SEPERATE WORDS
int j = 0;
char *p = strtok(command, " "); //DIVIDING BY SPACE
while (p != NULL)
{
token[j++] = p;
p = strtok(NULL, " "); //DIVIDING BY SPACE
}
token[j] = NULL;
// HERE IT WAS BREAKING AND WORKING INTO TOKENS
// CHECKING AND PRINTING ENVIRONMENTAL VARIABLES
char *duplicate; // AGAIN STORING INTO A DUPLICATE VARIABLE WHICH I WILL USE TO CHECK DIRECT
strcpy(duplicate, ptr);
int storej = j;
// IF cd_command == 0 then i have done implemnetation of cd downward
int cd_command = strncmp(duplicate, "cd", 2);
int home_compare = strcmp(duplicate, "printenv HOME");
// IMPLEMENTING cd ~ BECAUSE ~ is not recognized Directly by chdir
if(strncmp(duplicate,"help",4)==0)
{
printf("cd\nhistory\nhelp\nprintenv\nsetenv\necho\nexit\nquit\nx\t\n");
continue;
}
if(strncmp(duplicate,"cd ~",4)==0)
{
chdir(getenv("HOME"));
curr_path = getcwd(curr_path, 1000);
continue;
}
/* if(strncmp(duplicate,"setenv HOME",11)==0)
{ // JUST FOR CHECK
printf("Enter The Value U Want To Store >> ");
scanf("%[^\n]",duplicate);
setenv("HOME", duplicate, 1);
continue;
}
*/
// FOR SETTING UP environmental variable
// CHECKING IF THE INPUT IS SETENV
if(strncmp(duplicate,"setenv HOME",11)==0)
{
printf("Enter The Value U Want To Store >> ");
scanf("%[^\n]",duplicate);
setenv("HOME", duplicate, 1);
continue;
}
if(strncmp(duplicate,"setenv SHELL",11)==0)
{
printf("Enter The Value U Want To Store >> ");
scanf("%[^\n]",duplicate);
setenv("SHELL", duplicate, 1);
continue;
}
if(strncmp(duplicate,"setenv TERM",11)==0)
{
printf("Enter The Value U Want To Store >> ");
scanf("%[^\n]",duplicate);
setenv("TERM", duplicate, 1);