-
Notifications
You must be signed in to change notification settings - Fork 0
/
getdir.h
104 lines (78 loc) · 2.27 KB
/
getdir.h
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
//
//retrives the content of the dir and stores it in the info vector
//
#ifndef HEADER
#define HEADER
#include "header.h"
#endif
void getDir(){
struct stat sb;
struct dirent *dir;
DIR *d=opendir(cwd);
if (d==NULL) {
clrscreen();
goto(2,1);
cout<<"no path exists";
x++;
return;
}
struct tm *t;
vec.clear();
while((dir=readdir(d)) != NULL){
string str=(string)cwd+"/"+dir->d_name;
char *ch=new char[str.length()+1];
strcpy(ch,str.c_str());
if(stat(ch, &sb) == -1){
perror("lstat");
exit(EXIT_FAILURE);
}
string prm=getPermStr(sb,"");
struct passwd *pw = getpwuid(sb.st_uid);
struct group *gr = getgrgid(sb.st_gid);
string gown="";
string uown="";
string tim="";
if (pw != 0)
uown=(string)pw->pw_name;
if (gr != 0)
gown=(string)gr->gr_name;
t=gmtime(&sb.st_mtime);
long fsiz=sb.st_size;
if(fsiz>1024)fsiz=ceil(fsiz/1024);
vec.push_back({dir->d_name,fsiz,prm,gown,uown,getMon(t->tm_mon),t->tm_mday,t->tm_hour,t->tm_min});
}
top_cu=bottom_cu=0;
prntVec();
x=2;y=1;
coloredText();
closedir(d);
}
//----------returns file permissions string
string getPermStr(struct stat sb,string str){
S_ISDIR(sb.st_mode) ? str+="d" : str+="-";
sb.st_mode & S_IRUSR ? str+="r" : str+="-";
sb.st_mode & S_IWUSR ? str+="w" : str+="-";
sb.st_mode & S_IXUSR ? str+="x" : str+="-";
sb.st_mode & S_IRGRP ? str+="r" : str+="-";
sb.st_mode & S_IWGRP ? str+="w" : str+="-";
sb.st_mode & S_IXGRP ? str+="x" : str+="-";
sb.st_mode & S_IROTH ? str+="r" : str+="-";
sb.st_mode & S_IWOTH ? str+="w" : str+="-";
sb.st_mode & S_IXOTH ? str+="x" : str+="-";
return str;
}
//get month name
string getMon(int i){
if(i==0)return "JAN";
else if(i==1) return "FEB";
else if(i==2) return "MAR";
else if(i==3) return "APR";
else if(i==4) return "MAY";
else if(i==5) return "JUN";
else if(i==6) return "JUL";
else if(i==7) return "AUG";
else if(i==8) return "SEP";
else if(i==9) return "OCT";
else if(i==10) return "NOV";
else return "DEC";
}