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
283
284
285
286
287
|
#include "../command.h"
#include <errno.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <ftw.h>
#include <limits.h>
#define FILE_COLOR "\x1b[0m"
#define DIR_COLOR "\x1b[1;34m"
#define SET_UID_COLOR "\x1b[41m"
#define SET_GID_COLOR "\x1b[43m"
#define EXEC_COLOR "\x1b[1;92m"
#define LINK_COLOR "\x1b[1;96m"
struct Flags {
bool hidden;
bool hide_dot;
bool more_info;
bool one_column;
bool recurse;
};
static DIR* get_directory(char* path) {
DIR* d = opendir(path);
if (d == NULL) {
printf("\x1b[0merror: failed to open directory '%s': %s\n", path, strerror(errno));
}
return d;
}
static void append_path(char buf[PATH_MAX], const char* dir_path, const struct dirent* file) {
size_t dir_len = strlen(dir_path);
memcpy(buf, dir_path, dir_len);
if (buf[dir_len-1] != '/') {
buf[dir_len] = '/';
dir_len++;
}
memcpy(buf + dir_len, file->d_name, strlen(file->d_name) + 1);
}
static void list_file(struct dirent* file, struct Flags* flags, const char* dir_path) {
char* color;
if (file->d_type == DT_DIR) {
color = DIR_COLOR;
} else if (file->d_type == DT_LNK) {
color = LINK_COLOR;
} else {
color = FILE_COLOR;
}
if (flags->more_info) {
__uid_t uid = getuid();
__gid_t gid = getgid();
char buf[PATH_MAX];
append_path(buf, dir_path, file);
struct stat s;
if (stat(buf, &s) < 0) {
printf("\x1b[0merror: failed to read file '%s': %s\n", buf, strerror(errno));
return;
}
if (file->d_type == DT_DIR) putchar('d');
else if (file->d_type == DT_LNK) putchar('l');
else putchar('-');
bool group_sticky = false;
bool user_sticky = false;
bool exectuable = false;
putchar((s.st_mode & S_IRUSR) ? 'r' : '-');
putchar((s.st_mode & S_IWUSR) ? 'w' : '-');
if (s.st_mode & S_IXUSR) {
if (putchar(s.st_mode & S_ISUID)) {
putchar('s');
user_sticky = true;
} else {
putchar('x');
}
if (!exectuable) exectuable = s.st_uid == uid;
} else {
putchar('-');
}
putchar((s.st_mode & S_IRGRP) ? 'r' : '-');
putchar((s.st_mode & S_IWGRP) ? 'w' : '-');
if (s.st_mode & S_IXGRP) {
if (putchar(s.st_mode & S_ISGID)) {
putchar('s');
group_sticky = true;
} else {
putchar('x');
}
if (!exectuable) exectuable = s.st_gid == gid;
} else {
putchar('-');
}
putchar((s.st_mode & S_IROTH) ? 'r' : '-');
putchar((s.st_mode & S_IWOTH) ? 'w' : '-');
if (s.st_mode & S_IXOTH) {
putchar('x');
exectuable = true;
} else {
putchar('-');
}
printf(" %ld\t", s.st_nlink);
struct passwd* passwd = getpwuid(s.st_uid);
struct group* group = getgrgid(s.st_gid);
printf("%s ", passwd->pw_name);
printf("%s ", group->gr_name);
print_file_size(s.st_size);
print_date_time(s.st_mtim.tv_sec + s.st_mtim.tv_nsec / 1000000);
if (file->d_type == DT_REG) {
if (user_sticky) {
color = SET_UID_COLOR;
} else if (group_sticky) {
color = SET_GID_COLOR;
} else if (exectuable) {
color = EXEC_COLOR;
}
}
printf("%s%s\x1b[0m", color, file->d_name);
if (file->d_type == DT_LNK) {
char link[PATH_MAX];
if (readlink(buf, link, PATH_MAX) < 0) {
printf("\n");
} else {
printf(" -> %s\n", link);
}
} else {
printf("\n");
}
} else {
printf("%s%s", color, file->d_name);
flags->one_column ? putchar('\n') : putchar(' ');
}
}
static bool is_dot_dir(const char* path) {
return streql(path, ".") || streql(path, "..");
}
static void recurse_directory(char* path, struct Flags* flags) {
DIR* d;
struct dirent* file;
bool first = true;
d = get_directory(path);
if (d == NULL) return;
while((file = readdir(d)) != NULL) {
if (file->d_type == DT_DIR) continue;
if (!flags->hidden && prefix(".", file->d_name)) continue;
if (is_dot_dir(file->d_name)) continue;
if (first) {
printf("\n%s%s:%s\n", DIR_COLOR, path, FILE_COLOR);
first = false;
}
list_file(file, flags, path);
}
if (!flags->more_info) printf("\n");
closedir(d);
d = get_directory(path);
if (d == NULL) return;
while((file = readdir(d)) != NULL) {
if (file->d_type != DT_DIR) continue;
if (!flags->hidden && prefix(".", file->d_name)) continue;
if (is_dot_dir(file->d_name)) continue;
char buf[PATH_MAX];
append_path(buf, path, file);
recurse_directory(buf, flags);
}
closedir(d);
}
static void list_directory(char* path, struct Flags* flags) {
if (flags->recurse) {
recurse_directory(path, flags);
return;
}
DIR* d = get_directory(path);
if (d == NULL) return;
struct dirent* file;
while ((file = readdir(d)) != NULL) {
if (!flags->hidden && prefix(".", file->d_name)) continue;
if (flags->hide_dot && is_dot_dir(file->d_name)) continue;
list_file(file, flags, path);
}
closedir(d);
}
static void help() {
printf("Usage: ls [FILE]...\n\n");
printf("List directory contents\n\n");
printf("\t-1\tOne column output\n");
printf("\t-l\tLong format\n");
printf("\t-a\tInclude names starting with .\n");
printf("\t-A\tLike -a but without . and ..\n");
printf("\t-R\tRecurse\n");
exit(EXIT_SUCCESS);
}
COMMAND(ls) {
struct Flags flags;
flags.hidden = false;
flags.more_info = false;
flags.hide_dot = false;
flags.one_column = false;
flags.recurse = false;
int start = 0;
for (int i = 0; i < argc; i++) {
if (!prefix("-", argv[i])) break;
if (streql("--help", argv[i])) help();
start++;
for (size_t j = 1; j < strlen(argv[i]); j++) {
char c = argv[i][j];
switch (c) {
case 'R':
flags.recurse = true;
break;
case '1':
flags.one_column = true;
break;
case 'A':
flags.hide_dot = true;
flags.hidden = true;
break;
case 'a':
flags.hidden = true;
break;
case 'l':
flags.more_info = true;
break;
default:
error("error: unkown option -%c", c);
}
}
}
if (argc - start == 0) {
list_directory(".", &flags);
if (!flags.more_info) printf("\n");
return EXIT_SUCCESS;
}
bool titled = argc - start > 1;
for (int i = start; i < argc; i++) {
if (titled && !flags.recurse) {
printf("\n%s%s:%s\n", DIR_COLOR, argv[i], FILE_COLOR);
}
list_directory(argv[i], &flags);
if (titled && !flags.recurse) printf("\n");
}
if (!flags.more_info) printf("\n");
return EXIT_SUCCESS;
}
|