-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls.c
419 lines (333 loc) · 10.1 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/**
* Command to list computer files and directories.
* Copyright (C) 2024 Alexander (@alkuzin).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include "ls.h"
/* file names comparison function for qsort() */
static int cmplsfiles(const void *p1, const void *p2);
/* reversed file names comparison function for qsort() */
static int cmplsfiles_r(const void *p1, const void *p2);
/* file modifying time comparison function for qsort() */
static int cmplsfiles_t(const void *p1, const void *p2);
/* reversed file modifying time comparison function for qsort() */
static int cmplsfiles_rt(const void *p1, const void *p2);
/* file size comparison function for qsort() */
static int cmplsfiles_s(const void *p1, const void *p2);
/* reversed file size comparison function for qsort() */
static int cmplsfiles_rs(const void *p1, const void *p2);
/* get file type */
static char ls_get_type(mode_t mode);
/* get file mode/permissions */
static void ls_get_mode(char *buffer, mode_t mode);
/* unsinged int to strint lenght */
static size_t ls_utoa_len(uint32_t n);
/* get the largest file length for output alignment */
static int ls_get_max_size_len(ls_t *ls);
/* get the length for file with max number
of links for output alignment */
static int ls_get_max_nlink_len(ls_t *ls);
/* print directory contents in long list format */
static void ls_printdir_long_list_fmt(ls_t *ls);
/* print directory contents in a row */
static void ls_printdir_default(ls_t *ls);
static int cmplsfiles(const void *p1, const void *p2)
{
ls_file_t *file1, *file2;
char filename1[256];
char filename2[256];
file1 = (ls_file_t *)p1;
file2 = (ls_file_t *)p2;
strncpy(filename1, file1->filename, 256);
strncpy(filename2, file2->filename, 256);
for (int i = 0; filename1[i]; i++)
filename1[i] = tolower(filename1[i]);
for (int i = 0; filename2[i]; i++)
filename2[i] = tolower(filename2[i]);
return strncmp(filename1, filename2, 256);
}
static int cmplsfiles_r(const void *p1, const void *p2)
{
ls_file_t *file1, *file2;
char filename1[256];
char filename2[256];
file1 = (ls_file_t *)p1;
file2 = (ls_file_t *)p2;
strncpy(filename1, file1->filename, 256);
strncpy(filename2, file2->filename, 256);
for (int i = 0; filename1[i]; i++)
filename1[i] = tolower(filename1[i]);
for (int i = 0; filename2[i]; i++)
filename2[i] = tolower(filename2[i]);
return strncmp(filename2, filename1, 256);
}
static int cmplsfiles_rt(const void *p1, const void *p2)
{
ls_file_t *file1, *file2;
file1 = (ls_file_t *)p1;
file2 = (ls_file_t *)p2;
return ((file1->mtime > file2->mtime) - (file1->mtime < file2->mtime));
}
static int cmplsfiles_t(const void *p1, const void *p2)
{
ls_file_t *file1, *file2;
file1 = (ls_file_t *)p1;
file2 = (ls_file_t *)p2;
return ((file2->mtime > file1->mtime) - (file2->mtime < file1->mtime));
}
static int cmplsfiles_s(const void *p1, const void *p2)
{
ls_file_t *file1, *file2;
file1 = (ls_file_t *)p1;
file2 = (ls_file_t *)p2;
return (file2->size - file1->size);
}
static int cmplsfiles_rs(const void *p1, const void *p2)
{
ls_file_t *file1, *file2;
file1 = (ls_file_t *)p1;
file2 = (ls_file_t *)p2;
return (file1->size - file2->size);
}
static char ls_get_type(mode_t mode)
{
switch (S_IFMT & mode) {
case S_IFREG:
return '-';
case S_IFDIR:
return 'd';
case S_IFCHR:
return 'c';
case S_IFBLK:
return 'b';
case S_IFIFO:
return 'p';
case S_IFSOCK:
return 's';
case S_IFLNK:
return 'l';
default:
return '?';
}
}
static void ls_get_mode(char *buffer, mode_t mode)
{
buffer[0] = ls_get_type(mode);
buffer[1] = (S_IRUSR & mode) ? 'r' : '-';
buffer[2] = (S_IWUSR & mode) ? 'w' : '-';
buffer[3] = (S_IXUSR & mode) ? 'x' : '-';
buffer[4] = (S_IRGRP & mode) ? 'r' : '-';
buffer[5] = (S_IWGRP & mode) ? 'w' : '-';
buffer[6] = (S_IXGRP & mode) ? 'x' : '-';
buffer[7] = (S_IROTH & mode) ? 'r' : '-';
buffer[8] = (S_IWOTH & mode) ? 'w' : '-';
buffer[9] = (S_IXOTH & mode) ? 'x' : '-';
}
static size_t ls_utoa_len(uint32_t n)
{
size_t len;
len = 0;
if (n == 0)
return 1;
while (n >= 1) {
len++;
n /= 10;
}
return len;
}
static int ls_get_max_size_len(ls_t *ls)
{
int max_size_len;
size_t size;
max_size_len = 0;
for (size_t i = 0; i < ls->size; i++) {
size = ls_utoa_len(ls->files[i].size);
if ((size_t) max_size_len < size)
max_size_len = size;
}
return max_size_len;
}
static int ls_get_max_nlink_len(ls_t *ls)
{
int max_nlink_len;
size_t size;
max_nlink_len = 0;
for (size_t i = 0; i < ls->size; i++) {
size = ls_utoa_len(ls->files[i].nlink);
if ((size_t) max_nlink_len < size)
max_nlink_len = size;
}
return max_nlink_len;
}
void ls_init(ls_t *ls)
{
bzero(ls, sizeof(ls_t));
ls->dir = NULL;
ls->dp = NULL;
ls->size = 0;
ls->flags = 0;
}
void ls_opendir(ls_t *ls, const char *path)
{
ls->dir = opendir(path);
strncpy(ls->dirname, path, 256);
if (!ls->dir) {
printf("ls: cannot access \'%s\': %s\n", path, strerror(errno));
exit(EXIT_FAILURE);
}
}
void ls_readdir(ls_t *ls)
{
char *user, *group;
char filename[512];
size_t size, nbtime;
struct tm *mdate;
int status, i;
ls_file_t *file;
time_t mtime;
stat_t sb;
i = 0;
for (;;) {
ls->dp = readdir(ls->dir);
if (!ls->dp) {
if (errno) {
perror("ls: readdir");
exit(EXIT_FAILURE);
}
break;
}
bzero(filename, sizeof(filename));
strncpy(filename, ls->dirname, 256);
strncat(filename, ls->dp->d_name, 256);
if (!(LS_FLAG_ALL & ls->flags)) {
if (strncmp(ls->dp->d_name, ".", 1) == 0 || strncmp(ls->dp->d_name, "..", 2) == 0) {
ls->size--;
continue;
}
}
status = lstat(filename, &sb);
if (status == -1) {
printf("FILENAME: %s\n", filename);
perror("ls: stat error");
exit(EXIT_FAILURE);
}
file = &ls->files[i];
user = getpwuid(sb.st_uid)->pw_name;
group = getgrgid(sb.st_gid)->gr_name;
size = sb.st_size;
mtime = sb.st_mtime;
mdate = localtime(&mtime);
strncpy(file->filename, ls->dp->d_name, strlen(ls->dp->d_name));
strncpy(file->group, group, strlen(group));
strncpy(file->user, user, strlen(user));
ls_get_mode(file->mode, sb.st_mode);
file->nlink = sb.st_nlink;
file->size = size;
file->mtime = mtime;
nbtime = strftime(file->date, sizeof(file->date), "%b %e %R", mdate);
if (!nbtime) {
perror("ls: strftime");
exit(EXIT_FAILURE);
}
i++;
}
ls->size = i;
if (LS_FLAG_TIME & ls->flags) {
if (LS_FLAG_REVERSE & ls->flags)
qsort(ls->files, ls->size, sizeof(ls_file_t), cmplsfiles_rt);
else
qsort(ls->files, ls->size, sizeof(ls_file_t), cmplsfiles_t);
}
else if (LS_FLAG_SIZE & ls->flags) {
if (LS_FLAG_REVERSE & ls->flags)
qsort(ls->files, ls->size, sizeof(ls_file_t), cmplsfiles_rs);
else
qsort(ls->files, ls->size, sizeof(ls_file_t), cmplsfiles_s);
}
else {
if (LS_FLAG_REVERSE & ls->flags)
qsort(ls->files, ls->size, sizeof(ls_file_t), cmplsfiles_r);
else
qsort(ls->files, ls->size, sizeof(ls_file_t), cmplsfiles);
}
}
static void ls_printdir_long_list_fmt(ls_t *ls)
{
int max_size_len, max_nlink_len;
ls_file_t *file;
max_size_len = ls_get_max_size_len(ls);
max_nlink_len = ls_get_max_nlink_len(ls);
for (size_t i = 0; i < ls->size; i++) {
file = &ls->files[i];
printf("%s %*lu %s %s %*lu %-5s %s\n", file->mode, max_nlink_len, file->nlink, file->user,
file->group, max_size_len, file->size, file->date, file->filename);
}
}
static void ls_printdir_default(ls_t *ls)
{
char *filename;
for (size_t i = 0; i < ls->size; i++) {
filename = ls->files[i].filename;
if (strchr(filename, ' '))
printf("\'%s\'\n", filename);
else
printf("%s\n", filename);
}
}
void ls_printdir(ls_t *ls)
{
if (LS_FLAG_LONG_LIST & ls->flags)
ls_printdir_long_list_fmt(ls);
else
ls_printdir_default(ls);
}
void ls_destroy(ls_t *ls)
{
int status;
status = closedir(ls->dir);
if (status == -1) {
perror("ls: error to close directory");
exit(EXIT_FAILURE);
}
}
void ls_help(void)
{
puts(
"\nUSAGE\n"
"\tls [options] <file>\n\n"
"DESCRIPTION\n"
"\tls - list information about the <file>s (the current directory by default)\n\n"
"OPTIONS\n"
"\t-l\t\tuse a long listing format\n"
"\t-a\t\tdo not ignore entries starting with .\n"
"\t-r\t\treverse order while sorting\n\n"
"\t-t\t\tsort by time, newest first\n"
"\t-S\t\tsort by file size, largest first\n"
"\t--help\t\tdisplay this help and exit\n"
);
exit(EXIT_SUCCESS);
}