-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistDirectories.cpp
124 lines (106 loc) · 3.05 KB
/
listDirectories.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/********************************************************************************/
/* Name: Priyendu Mori */
/* Roll no: 2018201103 */
/********************************************************************************/
#include "header.h"
#include "macro.h"
extern vector<string> filelist;
extern string currentPath,home;
extern ofstream myfile;
/*
this function takes a directory name and lists out
all the files and directories inside it with information
like permissions, size, last modified, owner
*/
int listContent(string name){
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
filelist.clear();
cls;
pos(0,0);
string s=name;
if(s.compare(".") != 0) currentPath=name;
DIR *dir;
struct dirent *ent;
const char *dirname=name.c_str();
if ((dir = opendir (dirname)) != NULL) {
while ((ent = readdir (dir)) != NULL) {
displayContent(ent->d_name);
}
printf("\n");
closedir (dir);
pos(w.ws_row,0);
cout<<"Normal mode";
pos(0,0);
}
else {
return EXIT_FAILURE;
}
return 1;
}
/*
this function takes a directory name and
fetches all the required information,
formats it and displays on terminal
*/
void displayContent(const char *dname){
struct stat sb;
string dirname(dname);
if(dirname.compare("..")==0 && currentPath==home)
return;
string path=getPath(dname);
stat(path.c_str(), &sb);
printf((S_ISDIR(sb.st_mode)) ? "d" : "-");
printf((sb.st_mode & S_IRUSR) ? "r" : "-");
printf((sb.st_mode & S_IWUSR) ? "w" : "-");
printf((sb.st_mode & S_IXUSR) ? "x" : "-");
printf((sb.st_mode & S_IRGRP) ? "r" : "-");
printf((sb.st_mode & S_IWGRP) ? "w" : "-");
printf((sb.st_mode & S_IXGRP) ? "x" : "-");
printf((sb.st_mode & S_IROTH) ? "r" : "-");
printf((sb.st_mode & S_IWOTH) ? "w" : "-");
printf((sb.st_mode & S_IXOTH) ? "x" : "-");
printf("\t");
struct passwd *pwd = getpwuid(sb.st_uid);
struct group *grp = getgrgid(sb.st_gid);
const char *username=pwd->pw_name;
const char *groupname=grp->gr_name;
printf("%s %s \t", username, groupname);
long long size=sb.st_size;
char unit='B';
if(size>1024){
size/=1024;
unit='K';
}
if(size>1024){
size/=1024;
unit='M';
}if(size>1024){
size/=1024;
unit='G';
}
printf("%lld%c \t", size, unit);
string time = ctime(&sb.st_mtime);
string trimtime = time.substr(0, time.size()-1);
cout<< trimtime <<"\t";
printf ("%s \n", dname);
filelist.push_back(path);
}
/*
generates path for file named name
*/
string getPath(string name){
string path=currentPath;
path.append("/");
path.append(name);
return path;
}
/*
this function returns the current working directory
*/
string getpwd(){
char buff[FILENAME_MAX];
getcwd( buff, FILENAME_MAX );
string pwd(buff);
return pwd;
}