-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.c
155 lines (152 loc) · 5.29 KB
/
ls.c
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
#include"headers.h"
void func_ls(){
int i = 0;
struct dirent **lr;
int no = scandir(".", &lr, 0, alphasort); //lr points to allocated array of pointers to allocated strings
if (no >= 0){
for(i = 0; i < no; i++ ){
if(strcmp(lr[i]->d_name, ".") == 0 || strcmp(lr[i]->d_name, "..") == 0){
continue;
}
else{
printf("%s%s", WHITE, lr[i]->d_name);
}
printf("\n");
}
}
else{
perror ("Error");
}
//To free the Memory
for(i = 0; i < no; i++){
free(lr[i]);
}
free(lr);
//return;
}
void func_lsdir(char *name){
int i = 0;
struct dirent **lr;
int no = scandir(name, &lr, 0, alphasort);
if (no >= 0){
for(i = 0; i < no; i++ ){
if(strcmp(lr[i]->d_name, ".") == 0 || strcmp(lr[i]->d_name, "..") == 0){
continue;
}
else{
printf("%s%s", WHITE, lr[i]->d_name);
}
printf("\n");
}
}
else{
perror ("Error");
}
for(i = 0; i < no; i++){
free(lr[i]);
}
free(lr);
}
void func_lsa(){
int i = 0;
struct dirent **lr;
int no = scandir(".", &lr, 0, alphasort);
if (no >= 0){
for(i = 0; i < no; i++ ){
printf("%s%s ", WHITE, lr[i]->d_name);
printf("\n");
}
}
else{
perror ("Error");
}
for(i = 0; i < no; i++){
free(lr[i]);
}
free(lr);
}
void func_lsl(){
int i = 0, sum = 0;
char timer[14];
struct dirent **lr;
struct stat file;
int no = scandir(".", &lr, 0, alphasort);
if(no > 0){
for (i = 0; i < no; i++){
if(strcmp(lr[i]->d_name, ".") == 0 || strcmp(lr[i]->d_name, "..") == 0){
continue;
}
else if(stat(lr[i]->d_name, &file) == 0){
sum += file.st_blocks; // block size
// owner permissions-group permissions-other permissions
printf("%s%1s",WHITE, (S_ISDIR(file.st_mode)) ? "d" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IRUSR) ? "r" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IWUSR) ? "w" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IXUSR) ? "x" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IRGRP) ? "r" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IWGRP) ? "w" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IXGRP) ? "x" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IROTH) ? "r" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IWOTH) ? "w" : "-");
printf("%s%1s ", WHITE, (file.st_mode & S_IXOTH) ? "x" : "-");
// links associated - owner name - group name
printf("%s%2ld ", WHITE, (unsigned long)(file.st_nlink));
printf("%s%s ", WHITE, (getpwuid(file.st_uid))->pw_name);
printf("%s%s ", WHITE, (getgrgid(file.st_gid))->gr_name);
// file size (bytes) - time modified - name
printf("%s%5lld ", WHITE, (unsigned long long)file.st_size);
strftime (timer, 14, "%h %d %H:%M", localtime(&file.st_mtime));
printf("%s%s ", WHITE, timer);
printf("%s%s\n", WHITE, lr[i]->d_name);
}
}
}
else{
printf("%sEmpty\n", WHITE);
}
for(i = 0; i < no; i++){
free(lr[i]);
}
free(lr);
}
void func_lsal(){
int i = 0, sum = 0;
char timer[14];
struct dirent **lr;
struct stat file;
int no = scandir(".", &lr, 0, alphasort);
if(no > 0){
for (i = 0; i < no; i++){
if(stat(lr[i]->d_name, &file) == 0){
sum += file.st_blocks; // block size
// owner permissions-group permissions-other permissions
printf("%s%1s", WHITE, (S_ISDIR(file.st_mode)) ? "d" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IRUSR) ? "r" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IWUSR) ? "w" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IXUSR) ? "x" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IRGRP) ? "r" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IWGRP) ? "w" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IXGRP) ? "x" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IROTH) ? "r" : "-");
printf("%s%1s", WHITE, (file.st_mode & S_IWOTH) ? "w" : "-");
printf("%s%1s ", WHITE, (file.st_mode & S_IXOTH) ? "x" : "-");
// links associated - owner name - group name
printf("%s%2ld ", WHITE, (unsigned long)(file.st_nlink));
printf("%s%s ", WHITE, (getpwuid(file.st_uid))->pw_name);
printf("%s%s ", WHITE, (getgrgid(file.st_gid))->gr_name);
// file size (bytes) - time modified - name
printf("%s%5lld ", WHITE, (unsigned long long)file.st_size);
strftime (timer, 14, "%h %d %H:%M", localtime(&file.st_mtime));
printf("%s%s ", WHITE, timer);
printf("%s%s\n", WHITE, lr[i]->d_name);
}
}
}
else{
printf("%sEmpty\n", WHITE );
}
for(i = 0; i < no; i++){
free(lr[i]);
}
free(lr);
}