-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
110 lines (103 loc) · 3.01 KB
/
main.cpp
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
//2018201005 Vatsal Soni
#include "config.h"
#define esc 27 // ASCII code of ESC
#define cls printf("\033[H\033[J") // For clear screen
string root;
int main(int argc,char **argv) // strating point of the project
{
cls;
struct dirent **namelist; // For storing list of directory/file list
struct termios initialrsettings, newrsettings; // For terminal settings
struct winsize w; // For terminal properties
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
int n;
char* path;
//printf("%d\n",argc);
tcgetattr(fileno(stdin), &initialrsettings);
newrsettings = initialrsettings; // load initial setting of terminal and keep it safe because at the end
newrsettings.c_lflag &= ~ICANON; //we need to revert back those settings. Switch to non-canonical mode
newrsettings.c_lflag &= ~ECHO; // Turn off displaying text
//printf("%d\n",argc);
if(argc < 1)
{
exit(EXIT_FAILURE);
}
else if (argc == 1)
{
n=scandir(".",&namelist,NULL,alphasort); //store all files and dirctories into namelist from specified path
}
else
{
n = scandir(argv[1], &namelist, NULL,alphasort);
}
if(n < 0)
{
perror("scandir");
exit(EXIT_FAILURE);
}
else
{
string s;
if(argc==1)
{
s=".";
path=new char[s.length()+1];
strcpy(path, s.c_str());
}
else
{
s=argv[1];
path=new char[s.length()+1];
strcpy(path, s.c_str());
}
root=s;
for(int i=0;i<n-1;i++)
{
if(string(namelist[i]->d_name)=="..")
{
int x=i;
while(x<n-1)
{
namelist[x]=namelist[x+1];
x++;
}
break;
}
}
n--;
int n1;
if(n<=w.ws_row-2)
n1=n-1;
else
n1=w.ws_row-2;
/*cout<<n1<<endl;
for(int i=0;i<n1;i++)
cout<<i<<endl;*/
for(int i=0;i<=n1;i++)
{
fileInfo(path,namelist[i]->d_name,0); //call file to display information of the file
}
n--;
}
if(tcsetattr(fileno(stdin), TCSAFLUSH, &newrsettings) != 0)
{
fprintf(stderr,"Could not set attributes\n");
}
else
{
navigate(n,namelist,newrsettings,initialrsettings,root);
}
cls;
cout<<"Thank You!!!"<<endl;
tcsetattr(fileno(stdin), TCSANOW, &initialrsettings);
return 0;
}
// http://www.cse.psu.edu/~kxc104/class/cmpen472/11f/hw/hw7/vt100ansi.htm
// struct dirent {
// ino_t d_ino; /* inode number */
// off_t d_off; /* offset to the next dirent */
// unsigned short d_reclen; length of this record
// unsigned char d_type; /* type of file; not supported
// by all file system types */
// char d_name[256]; /* filename */
// };