forked from aashutoshrathi/Elf
/
ls-l.h
202 lines (174 loc) · 5.36 KB
/
ls-l.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
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
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include<time.h>
#ifdef HAVE_ST_BIRTHTIME
#define birthtime(x) x.st_birthtime
#else
#define birthtime(x) x.st_ctime
#endif
#define FALSE 0
#define TRUE 1
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
#define BOLD "\e[1m"
#define BGBLACK "\e[40m"
extern int alphasort();
char pathname[MAXPATHLEN];
void lsl()
{
int count,i,j,k=0;
struct dirent **files;
struct stat fileStat;
int file_selectl();
if (getcwd(pathname, sizeof(pathname)) == 0 )
{
printf("Error getting pathn");
exit(0);
}
//printf("Current Working Directory = %s\n",pathname);
count = scandir(pathname, &files, file_selectl, alphasort);
if (count <= 0)
{
printf("No files in this directoryn");
exit(0);
}
//for ls -l(parent directory and creation time not added) moe like ls -g
for (i=1;i<count-1;i++)
{
if(files[i-1]->d_name[0]!='.')
{
stat(files[i-1]->d_name, &fileStat);
printf( (S_ISDIR(fileStat.st_mode)) ? "d" : "-");
printf( (fileStat.st_mode & S_IRUSR) ? "r" : "-");
printf( (fileStat.st_mode & S_IWUSR) ? "w" : "-");
printf( (fileStat.st_mode & S_IXUSR) ? "x" : "-");
printf( (fileStat.st_mode & S_IRGRP) ? "r" : "-");
printf( (fileStat.st_mode & S_IWGRP) ? "w" : "-");
printf( (fileStat.st_mode & S_IXGRP) ? "x" : "-");
printf( (fileStat.st_mode & S_IROTH) ? "r" : "-");
printf( (fileStat.st_mode & S_IWOTH) ? "w" : "-");
printf( (fileStat.st_mode & S_IXOTH) ? "x" : "-");
//struct timespec x=fileStat.birthtime(st);
//printf("\t%i\t", x);
//printf("\tlength is %d \t",files[i-1]->d_reclen);
//prints group
struct passwd *pwd;
pwd=getpwuid(fileStat.st_uid);
printf("\t%s\t",pwd->pw_name);
/*struct group *grp;
grp=getgrgid(fileStat.st_gid);
printf("%s",grp->gr_name);*/
//printf("%s %s/t",pwd->pw_name,grp->gr_name);
//prints file size
int length=files[i-1]->d_reclen/32;
if(files[i-1]->d_reclen<32 )
printf("\t%d \t",length+1);
else if(files[i-1]->d_reclen>32)
printf("\t%d \t",length+1);
else
printf("\t%d \t",1);
off_t a=fileStat.st_size;
printf("%d\t", a);
//pritning last modification date
char time[100];
strftime(time,sizeof(time),"%b %2d %H:%M",localtime(&fileStat.st_mtime));
printf("%s\t",time);
//checkl for directory files
if(checkl(files[i-1]->d_name)==1)
{
printf("%s%s",KBLU,BOLD);
}
//checkl for executable files
else if(isExecutablel(files[i-1]->d_name)==1)
{
printf("%s%s",KGRN,BOLD);
}
//checkl for linked file
else if(checkl(files[i-1]->d_name)==2)
{
printf("%s%s",KCYN,BOLD);
}
//checkl for device file
else if(checkl(files[i-1]->d_name)==3)
{
printf("%s%s%s",KYEL,BOLD,BGBLACK);
}
//checkl for graphic or image file
else if(checkl_ext(files[i-1]->d_name)==1)
{
printf("%s%s",KMAG,BOLD);
}
//checkl for archive file
else if(checkl_ext(files[i-1]->d_name)==2)
{
printf("%s%s",KRED,BOLD);
}
printf("%s \t",files[i-1]->d_name);
printf("\n");
printf("%s",KNRM);}
}
}
int file_selectl(struct direct *entry)
{
if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..") == 0))
return (FALSE);
else
return (TRUE);
}
int isExecutablel(const char *name)
{
struct stat sb;
if (stat(name, &sb) == 0 && sb.st_mode & S_IXUSR)
return 1;
else
return 0;
}
int checkl(const char *name)
{
struct stat buf;
int x;
x = lstat(name, &buf);
switch (buf.st_mode & S_IFMT)
{
case S_IFDIR: return 1; //checkl for directory file
case S_IFLNK: return 2; //checkl for a link file
case S_IFBLK: return 3; //checkl for Block Device
case S_IFCHR: return 3; //checkl for Character Device
return 0;
}
}
int checkl_ext(const char *name)
{
char *extension, *n;
n=strdup(name);
extension=strrchr(n,'.');
if (extension==NULL)
{
return 0;
}
extension++;
char i1[]="jpeg", i2[]="gif", i3[]="bmp", i4[]="tiff", i5[]="png", i6[]="jpg";
char a1[]="a", a2[]="ar", a3[]="sbx", a4[]="cpio", a5[]="tar", a6[]="shar", a7[]="LBR", a8[]="iso", a9[]="lbr", a10[]="mar";
if(strncmp(extension,i1,4)==0 || strncmp(extension,i2,3)==0 || strncmp(extension,i3,3)==0 || strncmp(extension,i4,4)==0 || strncmp(extension,i5,3)==0 || strncmp(extension,i6,3)==0)
{
return 1;
}
else if(strncmp(extension,a1,1)==0 || strncmp(extension,a2,2)==0 || strncmp(extension,a3,3)==0 || strncmp(extension,a4,4)==0 || strncmp(extension,a5,3)==0 || strncmp(extension,a6,4)==0 || strncmp(extension,a7,3)==0 || strncmp(extension,a8,3)==0 || strncmp(extension,a9,3)==0 || strncmp(extension,a10,3)==0)
{
return 2;
}
else
return 0;
}