-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.cpp
More file actions
681 lines (548 loc) · 14.9 KB
/
Copy pathls.cpp
File metadata and controls
681 lines (548 loc) · 14.9 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
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
#include "ls.h"
using namespace std;
struct history
{
vector<string> list;
int curIndex;
};
int curListLen=0;
int currentViewTopIndex=0;
int traverseFlag=0;
int cursorIndex=0; //the file on which cursor is present
vector<string> directoryItems; //list is directories
int scrollingFlag=0; //1 for down,2 for up
int gotoFlag;
int currentViewTerminalLastRow=0;
struct history dirHistory;
void initializeHistory(){
dirHistory.curIndex=-1;
}
//to add to history when new directory is visited
void addToHistory(char path[]){
string s_path=string(path);
dirHistory.list.push_back(s_path);
dirHistory.curIndex++;
}
void setScrollingFlag(int a){
scrollingFlag=a; //1 for down, 2 for up.
//To be able to differentiate up whether to move the list up or down
}
int getCurrentViewTerminalLastRow(){
//index of the file that is displayed on the last row currently
return currentViewTerminalLastRow;
}
void setTraverseFlag(){
//travsing through the history using arrow keys
traverseFlag=1;
}
void resetTraverseFlag(){
traverseFlag=0;
}
void incrIndex(){
cursorIndex++;
}
int getIndex(){
return cursorIndex;
}
void decrIndex(){
if(cursorIndex>0){
cursorIndex--;
}
else{
cout << "Error. Index is 0";
}
}
int getCurListLen(){
return curListLen;
}
int incrCurrentViewTopIndex(){
//when the list needs to be refreshed on down arrow at last row
if(currentViewTopIndex>=0){
currentViewTopIndex++;
return 1;
}
else{
return 0;
}
}
void decrCurrentViewTopIndex(){
//when the list needs to be refreshed on up arrow at first row
currentViewTopIndex--;
}
int getCurrentViewTopIndex(){
return currentViewTopIndex;
}
//void printStatusBar(S_windowsize.ws_row)-2
int myLS(char path[PATH_MAX]){ //lists given directory with properties
if(scrollingFlag){ //scroling with up and down => current dir is same
strcpy(path, dirHistory.list[dirHistory.curIndex].c_str());
}
DIR *directory; // to open directory
char fullpath [PATH_MAX];
directory=opendir(path);
if(directory==NULL)
return -1;
if(!traverseFlag && !getMode() && !scrollingFlag){ //not traversing and not in cmd mode
//inserting to history
if(cursorIndex!=0 && cursorIndex!=1){ // if . or .. is not pressed
// addign to history
string s_path=string(path);
dirHistory.list.push_back(s_path);
dirHistory.curIndex++;
}
else if(cursorIndex==1){ // .. is pressed
dirHistory.curIndex--;
}
}
struct dirent *S_dirent; //dirent stucture
struct stat S_stat;
printf("\e[1;1H\e[2J"); //to clear screen
directoryItems.clear();
if( getMode() && gotoFlag)
{ //if in cmd mode and using goto option
//inserting to history
string s_path=string(path);
dirHistory.list.push_back(s_path);
dirHistory.curIndex++;
}
if(!scrollingFlag) //reset indicies if not in scrolling mode
{
cursorIndex=0;
currentViewTopIndex=0;
}
mode_t permission;
int i=0; //used to implement scrolling. index Variable for directory items
struct winsize S_windowsize; //used to fetch terminal size
ioctl(0, TIOCGWINSZ, &S_windowsize);
//S_dirent will have item name and inode
while((S_dirent=readdir(directory))!=NULL){
//directoryItems will contain the list of items of the currently visibly directory
directoryItems.push_back(S_dirent->d_name);
snprintf(fullpath, sizeof(fullpath), "%s/%s", path, S_dirent->d_name);
if(i>=currentViewTopIndex && i<((S_windowsize.ws_row)-2+currentViewTopIndex))
{ //to print only as many rows that can be viewed given teriminal size
//stat is used to fetch details about each directory item
if (stat(fullpath,&S_stat) == -1) {
printf("stat error");
break;
}
{
permission = S_stat.st_mode;
if(S_ISDIR(permission)){
printf("d");
}
else{
printf("-");
}
if( permission & S_IRUSR){
printf("r");
}
else{
printf("-");
}
if( permission & S_IWUSR){
printf("w");
}
else{
printf("-");
}
if( permission & S_IXUSR){
printf("x");
}
else{
printf("-");
}
if( permission & S_IRGRP){
printf("r");
}
else{
printf("-");
}
if( permission & S_IWGRP){
printf("w");
}
else{
printf("-");
}
if( permission & S_IXGRP){
printf("x");
}
else{
printf("-");
}
if( permission & S_IROTH){
printf("r");
}
else{
printf("-");
}
if( permission & S_IWOTH){
printf("w");
}
else{
printf("-");
}
if( permission & S_IXOTH){
printf("x");
}
else{
printf("-");
}
}
//printing size
printf(" \t %lld bytes ",S_stat.st_size);
//printing modified time
time_t S_time=S_stat.st_ctime;
struct tm lt;
localtime_r(&S_time, <);
char timbuf[80];
strftime(timbuf, sizeof(timbuf), "%c", <);
cout << " " << timbuf << " ";
//printing user name fetched from /usr/bin/passwd file
struct passwd *S_password;
S_password = getpwuid(S_stat.st_uid);
cout << " " << S_password->pw_name << " ";
//printing group
struct group *g= getgrgid(S_stat.st_gid);
cout << " " << g->gr_name << " " ;
printf("\t %s ",S_dirent->d_name);
printf("\n");
}
i++;
}
curListLen=directoryItems.size(); //used to implement scrolling
currentViewTerminalLastRow=S_windowsize.ws_row-3;
if(getMode()){
cout << "\e[1mCommand Mode\e[0m" <<endl;
}
else{
cout << "\e[1mNormal Mode\e[0m" <<endl ;
}
if(!scrollingFlag && !getMode()){ //new directory entered
printf("\033[0;0H"); //move cursor to initial position
}
else if(getMode()){
//print string if cmd mode is activated
cout << getCmdBuffer() ;
//leave cursor there
}
else{ //scrolling
if(scrollingFlag==1) // scrolling down.
printf("\033[%d;0H",currentViewTerminalLastRow+1);
else if(scrollingFlag==2) // scrolling up
printf("\033[0;0H");
scrollingFlag=0;
}
//printStatusBar();
closedir(directory);
return 0;
}
//when enter is pressed
void showSelectedDir(){
//if .. is pressed in root dir nothing should happen
string s = dirHistory.list[dirHistory.curIndex];
if(strcmp(s.c_str(),getRoot())==0 && cursorIndex==1)
return;
//clearing if there is any unwanted stuff in history
if(!traverseFlag){
if(dirHistory.curIndex < dirHistory.list.size()-1){
dirHistory.list.erase(dirHistory.list.begin() + dirHistory.curIndex+1 ,dirHistory.list.end());
}
}
char fullpath [PATH_MAX];
s = directoryItems[cursorIndex];
char fileName[PATH_MAX]; //will have the name of the file
strcpy(fileName, s.c_str());
//current file
s = dirHistory.list[dirHistory.curIndex];
snprintf(fullpath, sizeof(fullpath), "%s/%s",s.c_str(), fileName);
struct stat S_stat;
if (stat(fullpath,&S_stat) == -1)
{
printf("stat error");
}
//fetching permission
mode_t permission = S_stat.st_mode;
//if it is a file, open it
if(!S_ISDIR(permission))
{
int pid = fork();
if (pid == 0) {
execl("/usr/bin/open", "open", fullpath, (char *)0);
exit(1);
}
}
else //open it if it is a directory
{
myLS(fullpath);
}
}
//used to process entered command.
//cmd tokens will contains the string tokens in the command
vector<string> cmdTokens;
vector<string> cmdTokensAbs;
void processCmd(string cmd){ //will be called when enter is pressed in cmd mode
char c_cmd[PATH_MAX];
char sep[] = " ";
char *substring;
strcpy(c_cmd, cmd.c_str());
int j=0;
for (substring = strtok(c_cmd, sep); substring; substring = strtok(NULL, sep), j++) {
if(j!=0)
{ //don't push token ':'
cmdTokens.push_back(string(substring));
}
}
convToAbsolute(); //updates the vector cmdTokens into absolute paths
//proccessing cmd
if(cmdTokens[0].compare("copy")==0)
{
for(int i=1;i<cmdTokens.size()-1;i++)
{
//to check if it is a directory or file
struct stat S_stat;
stat(cmdTokensAbs[i].c_str(),&S_stat);
mode_t permission = S_stat.st_mode;
if(S_ISDIR(permission))
{
string destination = cmdTokensAbs[cmdTokensAbs.size()-1];
char dest[PATH_MAX];
strcpy(dest, destination.c_str());
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
copy_dir(source,dest);
}
else
{
//appending file name to destination directory to create file/dir
string destination = cmdTokensAbs[cmdTokensAbs.size()-1]+"/"+cmdTokens[i];
char dest[PATH_MAX];
strcpy(dest, destination.c_str());
createFile(dest);
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
singleFileCopy(source,dest);
}
}
}
else if(cmdTokens[0].compare("delete_file")==0)
{
for(int i=1;i<cmdTokens.size();i++)
{ //each file specified
struct stat S_stat;
stat(cmdTokensAbs[i].c_str(),&S_stat);
mode_t permission = S_stat.st_mode;
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
//cout << source << " to " << dest;
if(!S_ISDIR(permission))
delete_file(source);
}
}
else if(cmdTokens[0].compare("delete_dir")==0)
{
for(int i=1;i<cmdTokens.size();i++)
{ //each file specified
struct stat S_stat;
stat(cmdTokensAbs[i].c_str(),&S_stat);
mode_t permission = S_stat.st_mode;
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
//delete if it a directory
if(S_ISDIR(permission))
delete_dir(source);
}
}
else if(cmdTokens[0].compare("create_file")==0)
{
for(int i=1;i<cmdTokens.size();i++)
{
struct stat S_stat;
stat(cmdTokensAbs[i].c_str(),&S_stat);
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
//delete if it a directory
createFile(source);
}
}
else if(cmdTokens[0].compare("create_dir")==0)
{
for(int i=1;i<cmdTokens.size();i++)
{
struct stat S_stat;
stat(cmdTokensAbs[i].c_str(),&S_stat);
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
//delete if it a directory
createDir(source);
}
}
else if(cmdTokens[0].compare("goto")==0)
{
if(cmdTokens.size() == 2)
{
char path[PATH_MAX];
strcpy(path,cmdTokensAbs[1].c_str());
struct stat S_stat;
stat(cmdTokensAbs[1].c_str(),&S_stat);
mode_t permission = S_stat.st_mode;
if(S_ISDIR(permission)){ //if the dest if a directory
//now ensuring that user cannot go out the application root using goto
int parentLength = cmdTokensAbs[1].find_last_of("/");
if(parentLength>=strlen(getRoot()-1))
{ //if the absolule more is less than root length
gotoFlag=1;
myLS(path);
gotoFlag=0;
}
}
}
}
else if(cmdTokens[0].compare("snapshot")==0)
{
if(cmdTokens.size() == 3)
{
char folder[PATH_MAX];
strcpy(folder,cmdTokensAbs[1].c_str());
char filePath[PATH_MAX];
strcpy(filePath,cmdTokensAbs[2].c_str());
snapShotHelper(folder,filePath);
// enterCmdMode();
}
}
//move omitted
else if(cmdTokens[0].compare("move")==0)
{
for(int i=1;i<cmdTokens.size()-1;i++)
{
struct stat S_stat;
stat(cmdTokensAbs[i].c_str(),&S_stat);
mode_t permission = S_stat.st_mode;
if(S_ISDIR(permission)) //source must be a dir
{
string destination = cmdTokensAbs[cmdTokensAbs.size()-1];
char dest[PATH_MAX];
strcpy(dest, destination.c_str());
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
stat(destination.c_str(),&S_stat);
permission = S_stat.st_mode;
if(S_ISDIR(permission)) //destination also must be a dir
{
copy_dir(source,dest);
delete_dir(source);
}
}
else
{
//appending file name to destination directory to create file/dir
string destination = cmdTokensAbs[cmdTokensAbs.size()-1]+"/"+cmdTokens[i];
char destt[PATH_MAX];
strcpy(destt, destination.c_str());
createFile(destt);
char source[PATH_MAX];
strcpy(source, cmdTokensAbs[i].c_str());
stat(destination.c_str(),&S_stat);
permission = S_stat.st_mode;
if(S_ISDIR(permission)) //destination also must be a dir
{
singleFileCopy(source,destt);
delete_file(source);
}
}
//delete [] source, dest;
}
}
else if(cmdTokens[0].compare("rename")==0){
if(cmdTokensAbs.size()==3)
{
rename(cmdTokensAbs[1].c_str(),cmdTokensAbs[2].c_str());
}
}
cmdTokens.clear();
cmdTokensAbs.clear();
return;
}
void convToAbsolute(){ //updated the vector cmdTokens
cmdTokensAbs.push_back(cmdTokens[0]); //to match the indices b/w cmdToken and this
for(int i=1;i<cmdTokens.size();i++){
string temp = cmdTokens[i];
if(temp[0]== '/'){
char* t1=getRoot();
string root_string=string(t1);
cmdTokensAbs.push_back(root_string+cmdTokens[i]);
if(temp.size()>1)
cmdTokens[i]=temp.substr(1); //removing / so that only file name will be present
}
else if(temp[0]=='.' && temp[1]=='.'){
string currentDir = dirHistory.list[dirHistory.curIndex];
int baseLength = currentDir.find_last_of("/");
cmdTokensAbs.push_back(currentDir.substr(0,baseLength));
}
else if(temp[0]=='~'){
char* t1=getRoot();
string root_string =string(t1);
cmdTokensAbs.push_back(root_string+cmdTokens[i].substr(1));
if(temp.size()>1)
cmdTokens[i]=cmdTokens[i].substr(2);
//removing / so that only file name will be present. Will only file name to append
}
else if(temp[0]=='.'){
string currentDir = dirHistory.list[dirHistory.curIndex];
cmdTokensAbs.push_back(currentDir+cmdTokens[i].substr(1));
if(temp.size()>1) // if it is ./file1, /file1 will be place in cmdtoken
cmdTokens[i]=cmdTokens[i].substr(1);
}
else{ //just file name
string currentDir = dirHistory.list[dirHistory.curIndex];
cmdTokensAbs.push_back(currentDir+"/"+cmdTokens[i]);
}
}
}
void leftArrowPressed(){ //loading previously visited directory
setTraverseFlag();
if(dirHistory.curIndex > 0){
char path[PATH_MAX];
dirHistory.curIndex--;
string temp = dirHistory.list[dirHistory.curIndex];
strcpy(path,temp.c_str());
myLS(path);
}
resetTraverseFlag();
}
void rightArrowPressed(){
setTraverseFlag();
if(dirHistory.curIndex < dirHistory.list.size()-1){
char path[PATH_MAX];
dirHistory.curIndex++;
string temp = dirHistory.list[dirHistory.curIndex];
strcpy(path,temp.c_str());
myLS(path);
}
resetTraverseFlag();
}
void backSpacePressed(){ //in normal mode, go one directoroy up
string currentDirectory = dirHistory.list[dirHistory.curIndex]; //current dir
int baseLength = currentDirectory.find_last_of("/");
if(baseLength>=strlen(getRoot())) //making sure we don't go out of root dir
{
string s_path=currentDirectory.substr(0,baseLength);
dirHistory.list.push_back(s_path); //Store in history
dirHistory.curIndex++;
char path1[PATH_MAX]; //just for conversion purpose (from string to char*)
strcpy(path1,s_path.c_str());
myLS(path1);
}
}
void enterCmdMode(){ //just refreshes screen. Mode and cmdBuffer are set in main
char path[PATH_MAX];
string s = dirHistory.list[dirHistory.curIndex];
strcpy(path,s.c_str());
myLS(path);//refresh
}
void exitCmdMode(){//just refreshes screen. Mode is reset in in main
char path[PATH_MAX];
string s = dirHistory.list[dirHistory.curIndex];
strcpy(path,s.c_str());
myLS(path);//refresh
}