-
Notifications
You must be signed in to change notification settings - Fork 46
/
ls.c
284 lines (246 loc) · 6.26 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdio.h>
#include <dirent.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <bsd/string.h>
struct time_struct{
int date;
int month;
int currYear;
int hours;
int minutes;
int seconds;
};
// Function to convert unix time to
// Human readable format
int parse_unix_time(unsigned int seconds, struct time_struct* time)
{
// Number of days in month
// in normal year
int daysOfMonth[] = { 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
int currYear, daysTillNow, extraTime,
extraDays, index, date, month, hours,
minutes, secondss, flag = 0;
// Calculate total days unix time T
daysTillNow = seconds / (24 * 60 * 60);
extraTime = seconds % (24 * 60 * 60);
currYear = 1970;
// Calculating currrent year
while (daysTillNow >= 365) {
if (currYear % 400 == 0
|| (currYear % 4 == 0
&& currYear % 100 != 0)) {
daysTillNow -= 366;
}
else {
daysTillNow -= 365;
}
currYear += 1;
}
// Updating extradays because it
// will give days till previous day
// and we have include current day
extraDays = daysTillNow + 1;
if (currYear % 400 == 0
|| (currYear % 4 == 0
&& currYear % 100 != 0))
flag = 1;
// Calculating MONTH and DATE
month = 0, index = 0;
if (flag == 1) {
while (true) {
if (index == 1) {
if (extraDays - 29 < 0)
break;
month += 1;
extraDays -= 29;
}
else {
if (extraDays
- daysOfMonth[index]
< 0) {
break;
}
month += 1;
extraDays -= daysOfMonth[index];
}
index += 1;
}
}
else {
while (true) {
if (extraDays
- daysOfMonth[index]
< 0) {
break;
}
month += 1;
extraDays -= daysOfMonth[index];
index += 1;
}
}
// Current Month
if (extraDays > 0) {
month += 1;
date = extraDays;
}
else {
if (month == 2 && flag == 1)
date = 29;
else {
date = daysOfMonth[month - 1];
}
}
// Calculating HH:MM:YYYY
hours = extraTime / 3600;
minutes = (extraTime % 3600) / 60;
secondss = (extraTime % 3600) % 60;
time->date = date;
time->month = month;
time->currYear = currYear;
time->hours = hours;
time->minutes = minutes;
time->seconds = secondss;
return 0;
}
struct user_name{
uid_t uid;
char *str;
};
// currently user names are predefined cuz I am lazy
struct user_name usernames[] = {
{0, "root"},
{0}
};
char *get_user_name(uid_t uid){
struct user_name* p = usernames;
while (p)
{
if(p->uid == uid)
return p->str;
p++;
}
return NULL;
}
#define get_group_name(id) (get_user_name(id))
#define SHOW_HIDDEN 1
#define LONG_FORMAT 2
#define HUMAN_FORMAT 4
static char buffer[128];
static char permission_char[] = "-xwr";
void print_long_format(char *pathname, int flag){
struct stat statbuf;
struct time_struct time;
char *p = buffer;
int i, j, k;
int ret;
off_t size;
mode_t mode;
char *username, *groupname;
ret = stat(pathname, &statbuf);
if(ret)
return;
mode = statbuf.st_mode;
*p++ = S_ISDIR(statbuf.st_mode) ? 'd' : '-';
k = 0400;
for(i = 3; i > 0; i--){
for(j = 3; j > 0; j--){
*p++ = k & mode ? permission_char[j] : '-';
// printf("k %x mode %x\n", k, mode);
k = k >> 1;
}
}
*p = '\0';
username = get_user_name(statbuf.st_uid);
groupname = get_group_name(statbuf.st_gid);
printf("%s %2d %s %s ", buffer, statbuf.st_nlink, username, groupname);
size = statbuf.st_size;
if(flag & HUMAN_FORMAT){
off_t rem = size % 1024;
rem = rem >= 1000 ? 999 : rem;
printf("%2d.%.3dKB ", size / 1024, rem);
}else{
printf("%5d ", size);
}
parse_unix_time(statbuf.st_atime, &time);
printf("%02d/%02d/%04d %02d:%02d:%02d %s\n", time.date, time.month, time.currYear,
time.hours, time.minutes, time.seconds, pathname);
}
#define PATH_LEN (50)
int do_ls(char* pathname, int flag){
DIR* directory;
struct dirent* dir;
char *slash = "/";
char *symbol;
char path_buffer[PATH_LEN];
if(pathname == NULL)
pathname = ".";
directory = opendir(pathname);
if(!directory){
perror("opendir");
return 1;
}
while((dir = readdir(directory)) != NULL){
if(*dir->d_name == '.' && !(flag & SHOW_HIDDEN)){
continue;
}
if(flag & LONG_FORMAT){
strlcpy(path_buffer, pathname, PATH_LEN);
strlcat(path_buffer, "/", PATH_LEN);
strlcat(path_buffer, (char *)dir->d_name, PATH_LEN);
print_long_format(path_buffer, flag);
}else{
symbol = (dir->d_type == DT_DIR && *dir->d_name != '.') ? slash : "";
printf("%s%s ", symbol, (char *)dir->d_name);
}
}
if (!(flag & LONG_FORMAT)){
printf("\n");
}
closedir(directory);
return 0;
}
void usage(){
fprintf(stderr, "ls [-l] [-a] [-h] FOLDER\n");
exit(1);
}
int main(int argc, char*argv[]){
char* path = ".";
int ret, flag = 0;
int k;
char *cp;
/* Get flags. */
k = 1;
cp = argv[1];
if (cp && *cp++ == '-')
{
k++; /* points to first file */
while (*cp != 0)
{
switch (*cp)
{
case 'a':
flag |= SHOW_HIDDEN;
break;
case 'l':
flag |= LONG_FORMAT;
break;
case 'h':
flag |= HUMAN_FORMAT;
break;
default:
usage();
}
cp++;
}
}
if(k < argc){
path = argv[k];
}
ret = do_ls(path, flag);
// printf("ls opened %d %s\n", ret, path);
return ret;
}